Android memory allocation

I am getting a "bitmap size exceeds VM budget" error. I have read that there is a 16MB memory limit. In this thread Romain Guy says that "you can only allocate 16 MB of memory for your entire application".

However, my app must be running out of memory long before it reaches that limit. So my question is: how do I allocate memory to my application ... how can I get the allocation to my app increased (within the 16MB maximum)?


Solution 1:

As with any Java VM, the heap memory will automatically grow to the max size. But, bitmaps are allocated outside the VM, so you don't "see" them easily in the stats. The best thing you can do is make sure you don't use large bitmaps, or scale them down using
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

From Eclipse you can generate a heap dump when you are on Android 1.6 or up and you can analyze the dump with Eclipse MAT.

Generally you can't control the max heap size on a real device, unless you are working with custom hardware or firmware.

There should be an article at developer.android.com on dumping the heap on 1.6, but I'm unable to find it. :(

Edit
Also, I have to mention that you can request more memory for applications by using

android:largeHeap="true"

in the manifest. But this is highly ill-adviced as most applications do not need this.

Solution 2:

Note that the heap limit is device-dependent. On a Droid or Nexus One, that limit is 24 MB (to accommodate the larger graphic resources.)

Solution 3:

If you're using threads, then the debugger might be the source of the problem. If you run the app under the debugger, then any threads created will still be retained by the debugger, even when they're finished running. This leads to memory errors that won't occur when the app is running without the debugger.

http://code.google.com/p/android/issues/detail?id=7979

https://android.googlesource.com/platform/dalvik/+/master/docs/debugger.html