2

我正在尝试使用此处建议的 dalvikPss 值查找每个正在运行的应用程序和服务的相对内存使用情况

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

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

如果是这样,将返回的所有进程的 PSS 值相加是否ActivityManager.getRunningAppProcesses会给我所有正在运行的进程(包括服务和任务)的总 PSS?

4

1 回答 1

5

获取所有正在运行的进程的 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);
于 2012-04-09T11:17:09.687 回答