1.内存占用率
return String.format(Locale.ENGLISH, "%.2f", Double.parseDouble(getUsage("dumpsys meminfo -s "+t.getPackageName(),t.getPackageName()))/Double.parseDouble(Total())*100);
public String Total() {
String result = "";
try {
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
result = String.valueOf(mi.totalMem/1024);
} catch (Exception e) {
LogUtils.e(TAG+" Total memory :" + e.toString());
}
LogUtils.d(TAG + " result = " + result);
return (result != null) ? result:"";
}
串口命令
dumpsys meminfo 包名 |grep RSS
2.cpu占用率
getUsage("dumpsys cpuinfo",t.getPackageName());
串口命令
dumpsys cpuinfo |grep 包名
3.代码实现
public String getUsage(String command,String packageName) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
InputStream inputStream = process.getInputStream();
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inputStream));
String line = null;
while ((line = lnr.readLine()) != null ) {
if (command.contains("cpuinfo")){
if (line.contains(packageName)){
return line.split("%",2)[0];
}
}else if (command.contains("meminfo")){
if (line.contains("TOTAL RSS")){
return line.split("TOTAL RSS:",2)[1].split("T",2)[0];
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}