0

I want to detect the Memory and CPU consumption of a particular app in android (programmatically), can any one help me with it. I have tried the TOP method, but i want an alternative for it.

Any help will be appreciated, thanks :)

Ajinkya S
  • 570
  • 7
  • 17

2 Answers2

0

If you wan to trace your memory usage in your app then there is ActivityManager.getMemoryInfo() API.

Cpu usage can be traced using CpuStatsCollector API.

For more informative memory usage overview, outside your app, you can use adb shell dumpsys meminfo <package_name|pid> [-d] for more specific memory usage statistics. For example, there is the the command for com.google.android.apps.maps process:

adb shell dumpsys meminfo com.google.android.apps.maps -d

Which gives you a following output:

** MEMINFO in pid 18227 [com.google.android.apps.maps] **
               Pss  Private  Private  Swapped     Heap     Heap     Heap
             Total    Dirty    Clean    Dirty     Size    Alloc     Free
            ------   ------   ------   ------   ------   ------   ------
Native Heap    10468    10408        0        0    20480    14462     6017
Dalvik Heap    34340    33816        0        0    62436    53883     8553
Dalvik Other      972      972        0        0
    Stack     1144     1144        0        0
  Gfx dev    35300    35300        0        0
Other dev        5        0        4        0
 .so mmap     1943      504      188        0
 .apk mmap      598        0      136        0
 .ttf mmap      134        0       68        0
 .dex mmap     3908        0     3904        0
 .oat mmap     1344        0       56        0
 .art mmap     2037     1784       28        0
Other mmap       30        4        0        0
EGL mtrack    73072    73072        0        0
GL mtrack    51044    51044        0        0
  Unknown      185      184        0        0
    TOTAL   216524   208232     4384        0    82916    68345    14570

(output trimmed) More about it here

Tracing of memory usage on modern operating systems is very complex task. See this question for more info.

Community
  • 1
  • 1
plastique
  • 1,164
  • 2
  • 9
  • 19
-1

To get your processid:

int pid = android.os.Process.myPid();

To get CPU Usage :

public String getCPUUsage(int pid) {
   Process p;
   try {
   String[] cmd = {
      "sh",
      "-c",
      "top -m 1000 -d 1 -n 1 | grep \""+pid+"\" "};
   p = Runtime.getRuntime().exec(cmd);
   String line = reader.readLine();
   // line contains the process info
}
  • This code you can put in your app (you have to print the line in log), at certain places and or in a broadcast receiver, which you can trigger at certain instances and check the logs in the console. – Kundan Singh Thakur Jan 31 '20 at 06:31