android: how to get memory usage (ram) of a given PID programically

前端 未结 1 1745
走了就别回头了
走了就别回头了 2021-01-25 22:47

i want to log ram usage of a given application at a given time rate. i wrote the code to get the full memory value used but do not know how to get the memory usage of a given PI

相关标签:
1条回答
  • 2021-01-25 22:55
    ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
    List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();
    
    int[] pids = new int[procsInfo.size()];
    for (int i = 0; i < procsInfo.size(); i++) {
        ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
        pids[i] = info.pid;
    }
    
    Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
    // now walk the procsMemInfo array
    

    If you schedule a recurring timer to periodically re-query for running pids and query for memory info you can use timestamps to compute the memory usage over time.

    0 讨论(0)
提交回复
热议问题