在Android中,想要获得进程内存信息,有两类方法
1.exec大法,使用Runtime.getRuntime().exec()方法来执行命令行,主要命令行有 dumpsys(需要system权限) cat /proc等
private String catProc() {
StringBuilder meminfo = new StringBuilder();
try {
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("cat");
// commandLine.add("/proc/meminfo");
commandLine.add("/proc/" + android.os.Process.myPid() + "/status");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[commandLine.size()]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
meminfo.append(line);
meminfo.append("\n");
}
} catch (IOException e) {
Log.e(TAG, "Could not read /proc/meminfo", e);
}
Log.i(TAG, "showMeminfo = " + meminfo.toString());
return meminfo.toString();
}
2.android.os.Debug
Debug类有大量的获取内存信息方法,如getPss,用起来很简单