2

I am trying to find the relative memory usage of each running application and service using the dalvikPss value as suggested here

As suggested there, I need to sum up all the PSS values of running processes. My question is, how do get the pss values of all running processes ?

My understanding is that services and tasks in Android are linked to processes. Is this understanding correct ?

If so, would summing the PSS values of all processes returned from ActivityManager.getRunningAppProcesses give me the total PSS of all running processes (services and tasks included) ?

Community
  • 1
  • 1
Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
  • I have not. That was to be my last resort since starting a console off the application can have some bad performance implications right ? And apart from achieving this goal, I also want to know if my understanding that processes represent services and tasks is correct. Do you have any thoughts on that ? – Heshan Perera Apr 09 '12 at 11:10
  • yes right.then you have i think also tried using `android.os.Debug.MemoryInfo` – ρяσѕρєя K Apr 09 '12 at 11:15

1 Answers1

5

for getting PSS values of all running processes: use this:

ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);  
List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();  
Map<Integer, String> pidMap = new TreeMap<Integer, String>();  
for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses)  
{  
    pidMap.put(runningAppProcessInfo.pid, runningAppProcessInfo.processName);  
}  
Collection<Integer> keys = pidMap.keySet();  
for(int key : keys)  
{  
    int pids[] = new int[1];  
    pids[0] = key;  
    android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);  
    for(android.os.Debug.MemoryInfo pidMemoryInfo: memoryInfoArray)  
    {  
        Log.i(TAG, String.format("** MEMINFO in pid %d [%s] **\n",pids[0],pidMap.get(pids[0])));  
        Log.i(TAG, " pidMemoryInfo.getTotalPrivateDirty(): " + pidMemoryInfo.getTotalPrivateDirty() + "\n");  
        Log.i(TAG, " pidMemoryInfo.getTotalPss(): " + pidMemoryInfo.getTotalPss() + "\n");  
        Log.i(TAG, " pidMemoryInfo.getTotalSharedDirty(): " + pidMemoryInfo.getTotalSharedDirty() + "\n");  
    }  
} 

OR you can also get PSS value using Runtime:

    final Process m_process = Runtime.getRuntime().exec("/system/bin/top -n 1");
    final StringBuilder sbread = new StringBuilder();
    BufferedReader bufferedReader = new BufferedReader(new 
InputStreamReader(m_process.getInputStream()), 8192);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thank you very much... I saw the first example in another question as well. Are you suggesting that I sum the output of "getTotalPss()" ? What about the value in "dalvikPss" ? Most sources suggest using that value. The second answer involves starting a console right ? – Heshan Perera Apr 09 '12 at 11:25
  • @HeshanPerera : i have these two way for getting PSS value separately for each process then sum up.and second means not start a console which is vsible to user.we just running this command in background – ρяσѕρєя K Apr 09 '12 at 11:31
  • Thank you again. The latter statement I understand now. But what I dnt understand with the first method is, which value do I sum up ? You get output from three different functions right ? Out of those three functions, which ones do I sum up ? – Heshan Perera Apr 09 '12 at 11:37
  • @HeshanPerera :getTotalPss() method returning PSS value for each process. see code fist i'm extracting process pid and then passing in getProcessMemoryInfo for each process – ρяσѕρєя K Apr 09 '12 at 11:41
  • "My understanding is that services and tasks in Android are linked to processes. Is this understanding correct ?" Could you confirm whether than understanding is correct ? – Heshan Perera Apr 09 '12 at 12:41
  • Not clear to me using Total pss how one can get actual memory used by process. – sandy Nov 28 '12 at 13:07
  • @imrankhan my problem is how I can determine exact memory used by process like xx kb using pss and shared_Dirty and Private_Dirty any idea. – sandy Nov 28 '12 at 13:55
  • @ρяσѕρєяK I am only getting my app pss value, I am denied to read other running processes. Could you help me – Jyoti JK Feb 02 '18 at 04:14