3

Quite simply, how can I get the amount of memory (in MB) that my android app is currently using? This would need to be done in Java so I can display this information to the user.

I've looked at other stackoverflow posts but none give a simple or accurate answer to this problem.

getContext
  • 41
  • 1
  • 6
  • http://stackoverflow.com/questions/2298208/how-do-i-discover-memory-usage-of-my-application-in-android/2299813#2299813 – Zaartha Sep 28 '16 at 15:18
  • Try to use the ActivityManager: http://stackoverflow.com/questions/3170691/how-to-get-current-memory-usage-in-android – bigghe Sep 28 '16 at 15:25
  • Please define, in technical terms, what "actual RAM usage" means. [Quoting one of the core Android engineers](https://stackoverflow.com/a/2299813/115145): "Note that memory usage on modern operating systems like Linux is an extremely complicated and difficult to understand area. In fact the chances of you actually correctly interpreting whatever numbers you get is extremely low". – CommonsWare Sep 28 '16 at 15:58
  • ActivityManager and the approach given above only give the total memory, and memory available. They given no indication as to how much memory the app itself is consuming of the RAM. – getContext Oct 02 '16 at 17:13

4 Answers4

2

adb shell dumpsys meminfo packagename

Try to execute this command with Java.

currarpickt
  • 2,290
  • 4
  • 24
  • 39
user2539662
  • 130
  • 2
  • 10
0

You can use ActivityManager for that purpose.

It's answered in this post.

Community
  • 1
  • 1
djointster
  • 346
  • 3
  • 12
  • ActivityManager gives total memory and memory available. That is not what I'm looking for. I'm looking for how much memory the app itself is using at a particular time. – getContext Oct 02 '16 at 17:13
0

Try to use this code if a performance is not critical:

Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memInfo);
long res = memInfo.getTotalPrivateDirty();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
    res += memInfo.getTotalPrivateClean(); 

return res * 1024L;

If performance is critical check this answer

0

1. dumpsys meminfo PKG_NAME

...
App Summary
                      Pss(KB)
                       ------
          Java Heap:    42868  <--
        Native Heap:    52268  <--
               Code:    23608
              Stack:       96
           Graphics:     5084
      Private Other:     5620
             System:    14900

              TOTAL:   144444  <--    TOTAL SWAP PSS:      130

2. in java code

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Debug.java#640 // this is the value source of dumpsys meminfo.


  Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
  Debug.getMemoryInfo(memInfo);

  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    String javaMem = memInfo.getMemoryStat("summary.java-heap");
    String natiMem = memInfo.getMemoryStat("summary.native-heap");
    String totalMem = memInfo.getMemoryStat("summary.total-pss");

    //msg2 += "\n" + String.format("%s %s %s", javaMem, natiMem, totalMem);
    msg2 += "\n\n"+String.format("java %8s\nnati %8s\ntotal %8s",
        formatMB(Integer.parseInt(javaMem)), formatMB(Integer.parseInt(natiMem)), formatMB(Integer.parseInt(totalMem))
    );
  }

  ...

  String formatMB(double KB){
    return String.format("%.1f MB", KB/1024);
  }

The other api need lots of calculate,
// you can read them in Debug.java src file.
This is the closest to dumpsys meminfo or android studio monitor.

yurenchen
  • 1,897
  • 19
  • 17