Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect the heap size usage of an android application

Tags:

I would like to know the amount of heap space used by my android application in two ways: - programmatically - through DDMS.

I have referred to this post, prior to posting here. In that post, it is mentioned that, Debug.getNativeHeapSize() returns the heapsize. Is this the exact method I should use, in order to programmatically detect the heap size? If so, where should I log it in order to get the correct heapsize usage of my application?

like image 628
user264953 Avatar asked Jan 27 '11 18:01

user264953


People also ask

How do I discover memory usage of my application in android?

Tap Developer options and then tap Memory. In the resulting screen (Figure B), you'll see a list of the average memory used by the device in the past three hours (you can adjust the time frame, by tapping the time drop-down at the top). The Memory usage window in Android 12.

How do I monitor my heap size?

The easy way to monitor Heap usage is by using a commercial APM (Application Performance management tool) such as CA Wily APM, AppDynamics, New Relic, Riverbed, etc. APM tools not only monitor the heap usage, but you can also configure the tool to Alert you when Heap usage is not normal.


1 Answers

here's what I use:

public static void logHeap() {         Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576));         Double available = new Double(Debug.getNativeHeapSize())/1048576.0;         Double free = new Double(Debug.getNativeHeapFreeSize())/1048576.0;         DecimalFormat df = new DecimalFormat();         df.setMaximumFractionDigits(2);         df.setMinimumFractionDigits(2);          Log.d("tag", "debug. =================================");         Log.d("tag", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)");         Log.d("tag", "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");     } 
like image 183
zrgiu Avatar answered Oct 12 '22 02:10

zrgiu