Memory and CPU per process ANDROID

2019-04-17 04:01发布

问题:

I'm developing an android application where in it, I want to get the running process or app list. I kinda found a way to do using activitymanager. but what I don't know is on how to get the processor and memory used per that process. does any one know how I can achieve this goal.

the code to get the running processes is,

I get all the running app processes from the above code. please help me to get the values for each an every process in the list.

thank you.

回答1:

  1. CPU load - you need to read the /proc/[pid]/stat (ref http://linux.die.net/man/5/proc). Looking for utime & stime, they are the times the process spends on user mode and kernel mode. A good example to see how it works, you can find the source code of the top utility (check out the read_stat function in top.c).

  2. Memory usage - traditionally, people read /proc/[pid]/stat again for the rss field. Now it is more popular to use the PSS. (ref http://developer.android.com/reference/android/os/Debug.html#getPss%28%29). You can wiki the definition and difference of RSS and PSS - it is something to do with the size of global share library.