16

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

prepbgg
  • 3,564
  • 10
  • 39
  • 51

4 Answers4

36

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.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
botteaap
  • 5,790
  • 3
  • 29
  • 35
  • Thanks. I'll have a look at heap dumps and MAT when I get the time. – prepbgg Jan 25 '10 at 15:44
  • 1
    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? – prepbgg Jan 25 '10 at 22:18
  • 1
    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 Jan 26 '10 at 10:09
  • Thanks. I'll have a look at these suggestions when I have time. (The Android programming learning-curve gets steeper and steeper!) – prepbgg Jan 27 '10 at 17:36
  • 2
    android 1.5 works as well, check http://kohlerm.blogspot.com/2010/02/android-memory-usage-analysis-slides.html – kohlerm Feb 25 '10 at 08:54
  • 3
    android:largeHeap="true" does not work on the Application tag. Was it removed in GB? Do you know any alternative for that? – Gopinath Nov 23 '11 at 14:29
  • I was creating a custom tablet app, for use in a gallery. This really helped, as the design for the app worked so nicely with 25-30 high res images all loaded at the same time. Fair use case. And it is inside my application tag. – Chris Barry Jun 26 '12 at 18:17
  • @Gopinath it was added only in Honeycomb – dnet Jul 31 '12 at 12:54
11

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
  • 97,993
  • 18
  • 219
  • 200
6

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ThomasW
  • 16,981
  • 4
  • 79
  • 106
  • Thanks for this. I had wondered whether using the debugger might cause memory problems that might not otherwise occur. It is good to have it confirmed. – prepbgg Feb 09 '11 at 09:39
-1

I found the answer for your question recently.

Go to Android SDK Directory and
run sdk manager (Tools->android run this file)

In SDK manager go to Tools-->Manage AVDs

Now Android Virtual Device Manager Dialogue box is open..

in that window select your AVD and click Edit

Now in SD card section select SIZE and set 1024 MiB in Hardware section click New and select property " Maximum VM application heap Size" Now set 100 (based on your app requirement) Finally click "Edit AVD"

After edit click refresh button on sdk Manager

Now run your app in AVD your problem is solved.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Karthi Ponnusamy
  • 2,031
  • 2
  • 25
  • 41
  • ..and has really little (almost nothing) to do with memory available to your application and reducing memory bitmaps are using.. – Ewoks Jul 17 '15 at 07:10