0

In My app, I want to display list of running processes and memory allocated for those processes.

I tried many solutions discussed in SOF, finally I got this answer from this and this link. But the problem is, I don't know how to get Memory size in bytes from MemoryInfo. And This code runs if usage access is provided.

I tried this code,

    final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RunningAppProcessInfo> recentTasks = activityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo info : recentTasks) {
        Debug.MemoryInfo[] arrayofmem = activityManager.getProcessMemoryInfo(new int[]{info.pid});
        double size=0;
        for(Debug.MemoryInfo i:arrayofmem)
        {
            Debug.getMemoryInfo(i);
            size+=i.getTotalPss();
        }
    }

I don't understand the exception which is thrown,

W/ActivityManager: getRunningAppProcesses: caller 10240 does not hold REAL_GET_TASKS; limiting output
W/u.myapp: type=1400 audit(0.0:1756): avc: denied { read } for uid=10240 name="mem" dev="debugfs" ino=2051842 scontext=u:r:untrusted_app:s0 tcontext=u:object_r:debugfs:s0 tclass=file permissive=0

Please save me. Thanks in advance!!!

Jyoti JK
  • 2,141
  • 1
  • 17
  • 40

1 Answers1

1

First of all, the message you see in the logcat is not an exception, it is just a warning message. It says that your app doesn't hold the permission REAL_GET_TASKS which you would need if you wanted to get all information about all running processes. Since you don't have this permission, Android will return only certain information (basically you can get information about your own process, but not any others).

The values contained in Debug.MemoryInfo are all in Kilobytes.

See this answer for details on the values in Debug.MemoryInfo: https://stackoverflow.com/a/2299813/769265

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • But How can I get Real_get_tasks permission. I read that third party apps cannot hold that permission.I saw many apps(in playstore) which display the processes and their memory size – Jyoti JK Feb 01 '18 at 11:48