简体   繁体   English

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

[英]Android : How to get total PSS of all running processes

I am trying to find the relative memory usage of each running application and service using the dalvikPss value as suggested here我正在尝试使用此处建议的 dalvikPss 值查找每个正在运行的应用程序和服务的相对 memory 使用情况

As suggested there, I need to sum up all the PSS values of running processes.正如那里所建议的那样,我需要总结正在运行的进程的所有 PSS 值。 My question is, how do get the pss values of all running processes?我的问题是,如何获取所有正在运行的进程的 pss 值?

My understanding is that services and tasks in Android are linked to processes.我的理解是Android中的服务和任务是挂在进程上的。 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)?如果是这样,将从ActivityManager.getRunningAppProcesses返回的所有进程的 PSS 值求和会给我所有正在运行的进程(包括服务和任务)的总 PSS 吗?

for getting PSS values of all running processes: use this:获取所有正在运行的进程的 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");  
    }  
} 

OR you can also get PSS value using Runtime:或者您也可以使用运行时获取 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM