gpt4 book ai didi

安卓 : How to get total PSS of all running processes

转载 作者:行者123 更新时间:2023-11-29 16:15:13 24 4
gpt4 key购买 nike

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

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

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

如果是这样,将从 ActivityManager.getRunningAppProcesses 返回的所有进程的 PSS 值求和,是否可以得到所有正在运行的进程(包括服务和任务)的总 PSS?

最佳答案

用于获取所有正在运行的进程的 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);

关于安卓 : How to get total PSS of all running processes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com