views:

3705

answers:

3

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

+1  A: 

You could try using VMRuntime's setMinimumHeapSize, although as far as I know dalvik should automatically increase your Heap size when you run out of memmory (till it really runs out of memory :)). Have you tried profiling your memory usage with DDMS ? I really doubt that Android fails to give you more memory as long as it is capable of doing so.

Ivan
Thanks for the very quick response! Do you know of a dummies' guide to profiling memory allocation? I have tried using the "Track Allocation" feature in DDMS but found it difficult to interpret the results. The allocations appeared to be in size order with the largest at the top, but I couldn't see anything bigger than about 16k. My bitmap must be bigger than that. Is there any guidance on how to view and interpret the allocations?
Specifically, how can I see the total memory allocation and the breakdown of this in object types and individual objects?
+4  A: 

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 d.android.com on dumping the heap on 1.6, but I'm unable to find it :(

botteaap
Thanks. I'll have a look at heap dumps and MAT when I get the time.
I've just noticed that you said I could get a heap dump and use Eclipse MAT on Android 1.6. Unfortunately, my device is on 1.5 so that is what I am programming for. Is there any easy way of getting a memory analysis for 1.5? The memory usage chart in SysInfo in DDMS include shows a small increase in usage on each screen rotation; does it include the non-heap memory used by bitmaps? The DDMS Allocation Tracker does not show any significant memory usage (assuming its units are bytes); are there any objects other than bitmaps that would not show up in the DDMS Allocation Tracker?
You could either run your code in the 1.6 emulator for debugging, or you can try the procedure described here http://biowallet.blogspot.com/2009/04/analyze-android-15-memory-dump.html or you can use the api at http://developer.android.com/reference/android/os/Debug.html#dumpHprofData%28java.lang.String%29 to dump the heap from your app to a location where you like.
botteaap
Thanks. I'll have a look at these suggestions when I have time. (The Android programming learning-curve gets steeper and steeper!)
android 1.5 works as well, check http://kohlerm.blogspot.com/2010/02/android-memory-usage-analysis-slides.html
kohlerm
+2  A: 

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

Romain Guy