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 - 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.

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 =;
System.out.println("pName=" + pName);
System.out.println("pID=" + pID);
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);
int memSize = memoryInfo[0].getTotalPss();
System.out.println("memSize=" + memSize);
//you can't call getTotalUss directly, because it is @hide function:
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) {
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
return memSize;


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: pID=1962
System.out: memSize=33207
System.out: o=28544

No comments:

Post a comment