views:

1430

answers:

3

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 any case, I'm looking for solution that works for 1.5 and upwards.

A: 

Do you mean programatically, or just while you're developing and debugging? If the latter, you can see that info from the DDMS perspective in Eclipse. When your emulator (possibly even physical phone that is plugged in) is running, it will list the active processes in a window on the left. You can select it and there's an option to track the heap allocations.

Steve H
I mean programmatically. Clarified the question. Thanks.
hgpc
I suggest you look at this post by one of the Android platform programmers then: http://stackoverflow.com/questions/2298208/how-to-discovery-memory-usage-on-my-application-in-android/2299813#2299813
Steve H
A: 

Debug.getNativeHeapSize() will do the trick, I should think. It's been there since 1.0, though.

The Debug class has lots of great methods for tracking allocations and other performance concerns. Also, if you need to detect a low-memory situation, check out Activity.onLowMemory().

Neil Traft
Thanks Neil. Does this work when the application is running with debugging turned off?
hgpc
+1  A: 

This official API is:

http://developer.android.com/reference/android/app/ActivityManager.html#getMemoryClass()

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).

hackbod