Android:如何获得所有正在运行的进程的总PSS

时间:2012-04-09 10:34:14

标签: android memory

我正在尝试使用建议的here

使用dalvikPss值来查找每个正在运行的应用程序和服务的相对内存使用情况

正如那里所建议的,我需要总结正在运行的进程的所有PSS值。我的问题是,如何获取所有正在运行的进程的pss值?

我的理解是Android中的服务和任务与进程相关联。这种理解是否正确?

如果是这样,那么汇总从ActivityManager.getRunningAppProcesses返回的所有进程的PSS值会给出所有正在运行的进程(包括服务和任务)的总PSS吗?

1 个答案:

答案 0 :(得分:4)

获取所有正在运行的进程的PSS值: 用这个:

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");  
    }  
} 

或者您也可以使用运行时获取PSS值:

    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);