0

Although there are similar questions (such as A), their answers do not solve my problem.

I am using Android Studio 1.5.1 targeting Android API 18 (before Android KitKat 4.4, so I’m dealing with Dalvik, not ART runtime).

My question is:

In Android, how to find the total used memory (native and non-native) in bytes used by my app programmatically without using any native code?

Update:

Why other similar solutions do not solve my problem?

Here is the problem in more details:

I have two methods, before() and after(). Bothe methods have the same code that calculate the total used memory but method after() has an integer local variable

int consume = 123;

The problem is the after() method should show the total used memory + 4 (which is the size of the int variable consume), but instead of that both give me the same total used memory.

Community
  • 1
  • 1
User
  • 277
  • 4
  • 13
  • 1
    "their answers do not solve my problem" -- [Dianne Hackborn's answer](http://stackoverflow.com/a/2299813/115145), citing `getProcessMemoryInfo()` on `ActivityManager`, would certainly seem to be in the ballpark. And, as she notes, calculating memory consumption is problematic in general. Please explain, in detail, why what she has does not solve your problem. – CommonsWare Jan 17 '16 at 00:08
  • 1
    Possible duplicate of [How do I discover memory usage of my application in Android?](http://stackoverflow.com/questions/2298208/how-do-i-discover-memory-usage-of-my-application-in-android) – petey Jan 17 '16 at 00:10
  • I used getProcessMemoryInfo() to get the totalPSS in two methods (in the same process) before and after using some integer variables but the numbers are not correct. – User Jan 17 '16 at 01:10
  • 1
    You're not going to get a precise answer. The only figure that really matters is the number of 4096-byte pages allocated by the system. The exact number of bytes used will be somewhat less. Unless something your app does causes a new page to be allocated, you won't see any change in memory footprint. Your integer local variable is stored on the stack, not the heap, but it has similar properties (i.e. it has a fixed virtual address range but expands to fill physical pages as needed). Do you understand what PSS means? – fadden Jan 17 '16 at 06:21

1 Answers1

0

Not sure why the other answers didn't solve your problem, but this is what I am doing at the same API level.

public static void getMemoryStat(){

    double max = Runtime.getRuntime().maxMemory(); 
    double heapSize = Runtime.getRuntime().totalMemory(); 
    double heapRemaining = Runtime.getRuntime().freeMemory(); 
    double nativeUsage = Debug.getNativeHeapAllocatedSize(); 

    double totalMemoryUsed = (Runtime.getRuntime().totalMemory() + android.os.Debug.getNativeHeapAllocatedSize());
    int percentUsed = (int)(totalMemoryUsed / Runtime.getRuntime().maxMemory() * 100);
    double remaining = max - (heapSize - heapRemaining + nativeUsage); 
    ErrorLog.log("max:"+ max);
    ErrorLog.log("heapSize:"+ heapSize);
    ErrorLog.log("heapRemaining:"+ heapRemaining);
    ErrorLog.log("nativeUsage:"+ nativeUsage);
    ErrorLog.log("remaining:"+ remaining);
    ErrorLog.log("percentUsed:"+ percentUsed);

}

ErrorLog is my custom class. You should change it to Log.d()

Sandeep Kaul
  • 2,957
  • 2
  • 20
  • 36