How do you programmatically detect the application heap size available to an Android app?
I heard there\'s a function that does this in later versions of the SDK. In
The official API is:
This was introduced in 2.0 where larger memory devices appeared. You can assume that devices running prior versions of the OS are using the original memory class (16).
Here's how you do it:
Getting the max heap size that the app can use:
Runtime runtime = Runtime.getRuntime();
long maxMemory=runtime.maxMemory();
Getting how much of the heap your app currently uses:
long usedMemory=runtime.totalMemory() - runtime.freeMemory();
Getting how much of the heap your app can now use (available memory) :
long availableMemory=maxMemory-usedMemory;
And, to format each of them nicely, you can use:
String formattedMemorySize=Formatter.formatShortFileSize(context,memorySize);
Runtime rt = Runtime.getRuntime();
rt.maxMemory()
value is b
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.getMemoryClass()
value is MB