11

Can we rely on our Android app getting a set amount of memory or does it vary between phone models or OS versions?

For example, I have a memory cache, and I've set its limit to 5 MB. If on one device, my app will only get 8 MB of memory to play with, and on another, 24 MB, I'd like to tune the cap of the memory cache to take advantage of more or less memory.

Is there any way to figure this out, does the amount even vary?

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
user291701
  • 38,411
  • 72
  • 187
  • 285

2 Answers2

19

Yes, the maximum heap size varies from device to device. You can check the device's approximate per-application memory class by calling getMemoryClass().

Returns the approximate per-application memory class of the current device. This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best. The returned value is in megabytes; the baseline Android memory class is 16 (which happens to be the Java heap limit of those devices); some device with more memory may return 24 or even higher numbers.

The only built-in way to change heap size of an app is by setting android:largeHeap="true" in the AndroidManifest.xml. This will generally increase heap size from 48 to 128. Keep in mind this approach will only work on 3.x tablets. Otherwise, you'll need a rooted device, which is obviously not something you would want to rely on as a developer.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Cool, yeah I only want to know so that I can adjust my memcache limit as it makes sense. I'm not sure what "memory class" means though - does the returned integer represent the heap size in MB? My Wildfire returns 16, my galaxy nexus returns 64. Thanks – user291701 May 25 '12 at 03:27
  • In this case, the "memory class" represents the "maximum heap size". Note that the heap may or may not ever reach this size. The heap dynamically extends as the process needs more and more memory. The "max heap size" simply sets a limit as to how much the heap can grow. Let me know if this makes sense :). – Alex Lockwood May 25 '12 at 03:30
  • Hmm yeah slightly confused because my Wildfire reports memclass=16, but the app will throw an OOM exception way before hitting 16mb in heap usage. I just need to take a closer look at the mem analysis, thanks for your help! – user291701 May 25 '12 at 04:02
  • Interesting that my Galaxy Nexus returs 96 – Nemanja Kovacevic May 09 '13 at 10:05
0

It may help you

16 MB is the maximum memory limit given to any given android application. Some second generation phones will return 24 MB of memory for each process or even more.

What is the maximum memory limit given for each process or application in android?