通过adb采集app性能的native heap、dalvik heap、psstotal大小

之前在百度看了好几多资料,耐烦出现好多坑,估计是时间太久了,导致不适用,现在我贴上我的代码,大家看一下,本人已通过编译,可以正常运行

	public static String[] getHeap(String packageName) {
		String cmd = "adb shell dumpsys meminfo " + packageName;
		String PSS = null;
		String Native = null;
		String Dalvik = null;
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(cmd);
			InputStream in = p.getInputStream();
			BufferedReader bReader = new BufferedReader(new InputStreamReader(in));
			StringBuffer sb = new StringBuffer();
			String line = "";
			while ((line = bReader.readLine()) != null) {
				sb.append(line + " ");
				String data = sb.toString();
				// System.out.println(data);
				PSS = getPSS(data);
				if (PSS == null) {
					continue;
				}
				Native = getNative(data);
				if (Native == null) {
					continue;
				}
				Dalvik = getDalvik(data);
				if (Dalvik == null) {
					continue;
				}
			}
			bReader.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String[] result = { PSS, Native, Dalvik };
		return result;
	}

	public static String getPSS(String info) {
		String result = null;
		String p = ".TOTAL:(\\s+\\d+)";
		Pattern pattern = Pattern.compile(p);
		Matcher m = pattern.matcher(info);
		if (m.find()) {
			result = m.group(1).toString().trim();
		}
		return result;
	}

	public static String getNative(String info) {
		String result = null;
		String p = ".Native Heap:(\\s+\\d+)";
		Pattern pattern = Pattern.compile(p);
		Matcher m = pattern.matcher(info);
		if (m.find()) {
			result = m.group(1).toString().trim();
		}
		return result;
	}

	public static String getDalvik(String info) {
		String result = null;
		String p = ".Java Heap:(\\s+\\d+)";
		Pattern pattern = Pattern.compile(p);
		Matcher m = pattern.matcher(info);
		if (m.find()) {
			result = m.group(1).toString().trim();
		}
		return result;
	}

 

展开阅读全文

没有更多推荐了,返回首页