Android memory allocation
Asked Answered
N

4

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

Namedropping answered 25/1, 2010 at 11:40 Comment(1)
well 640KB shold be enough for anything google just being generous...Feebleminded
S
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.

Shaunshauna answered 25/1, 2010 at 12:35 Comment(8)
Thanks. I'll have a look at heap dumps and MAT when I get the time.Namedropping
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?Namedropping
You could either run your code in the 1.6 emulator for debugging, or you can try the procedure described here biowallet.blogspot.com/2009/04/… or you can use the api at developer.android.com/reference/android/os/… to dump the heap from your app to a location where you like.Shaunshauna
Thanks. I'll have a look at these suggestions when I have time. (The Android programming learning-curve gets steeper and steeper!)Namedropping
android 1.5 works as well, check kohlerm.blogspot.com/2010/02/…Deakin
android:largeHeap="true" does not work on the Application tag. Was it removed in GB? Do you know any alternative for that?Kopje
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.Sommer
@Kopje it was added only in HoneycombMaquis
H
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.)

Helmut answered 25/1, 2010 at 17:10 Comment(5)
Nexus S has 24MB as well, it seems a most high end devices will have more than 16MB but if you want to run on any device you should keep it under 16MB.Tuba
No, Nexus S has 32 MB on 2.3.Helmut
How do i get t o know the heap limit of my device?Shrew
Runtime.getRuntime().maxMemory();Potsdam
My Nexus S has 48 MB under ICS.Nitrogen
R
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

Roti answered 7/2, 2011 at 6:3 Comment(1)
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.Namedropping
S
-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.

Singlestick answered 21/8, 2012 at 6:49 Comment(1)
..and has really little (almost nothing) to do with memory available to your application and reducing memory bitmaps are using..Green

© 2022 - 2024 — McMap. All rights reserved.