Wednesday, 28 October 2015

Android, get system memory usage and CPU info



Majorly there are 3 different methods to do this:

1. by calling API. (may not support all your requirements. )

2. by access file system (because android is linux basically)

3. by executing shell and process the result (again, android is linux.)






内存耗用:VSS/RSS/PSS/USS
Terms
• VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
• RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
• PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
• USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS
USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaks in a process.




https://android.googlesource.com/platform/packages/apps/Camera2/+/master/src/com/android/camera/app/MemoryQuery.java

http://blog.csdn.net/l2show/article/details/40950657
http://blog.csdn.net/kieven2008/article/details/6445421
http://huangqinqin.iteye.com/blog/1731307
http://myeyeofjava.iteye.com/blog/2173569
http://www.cnblogs.com/yejiurui/p/3472765.html
http://www.gitzx.com/android-performancetest/




For memory:



public void getRAMInfo2 () {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> list = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info:
list) {
String pName = info.processName;
int pID = info.pid;
System.out.println("pName=" + pName);
System.out.println("pID=" + pID);
getPidMemorySize(pID,this);
}
}
public int getPidMemorySize(int pid, Context context)
{
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int[] myMempid = new int[] { pid };
Debug.MemoryInfo[] memoryInfo = am.getProcessMemoryInfo(myMempid);
memoryInfo[0].getTotalSharedDirty();
int memSize = memoryInfo[0].getTotalPss();
System.out.println("memSize=" + memSize);
//you can't call getTotalUss directly, because it is @hide function: http://baiduhix.blogspot.com/2015/10/what-does-hide-mean-in-android-source.html
Method m = null;
try {
m = Debug.MemoryInfo.class.getMethod("getTotalUss",null );
Object o = m.invoke(memoryInfo[0], null);
System.out.println("o=" + o);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return memSize;
}


result:


System.out: pName=com.android.launcher
System.out: pID=1724
System.out: memSize=41388
System.out: o=37420
System.out: pName=com.piriform.ccleaner
System.out: pID=5038
System.out: memSize=21630
System.out: o=19972
System.out: pName=com.google.android.gms
System.out: pID=1962
System.out: memSize=33207
System.out: o=28544
































No comments:

Post a comment