31

I've been doing a lot of searching and I know a lot of other people are experiencing the same OOM memory problems with BitmapFactory. My app only shows a total memory available of 4MB using Runtime.getRuntime ().totalMemory(). If the limit is 16MB, then why doesn't the total memory grow to make room for the bitmap? Instead it throws an error.

I also don't understand that if I have 1.6MB of free memory according to Runtime.getRuntime().freeMemory() why do I get an error saying "VM won't let us allocate 614400 bytes"? Seems to me I have plenty available memory.

My app is complete except for this problem, which goes away when I reboot the phone so that my app is the only thing running. I'm using an HTC Hero for device testing (Android 1.5).

At this point I'm thinking the only way around this is to somehow avoid using BitmapFactory.

Anyone have any ideas on this or an explanation as to why VM won't allocate 614KB when there's 1.6MB of free memory?

Swati Garg
  • 995
  • 1
  • 10
  • 21
Greg
  • 2,233
  • 4
  • 18
  • 7
  • [this](http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/14731953#14731953) may help! – Rupesh Yadav Feb 06 '13 at 15:14

5 Answers5

47

[Note that (as CommonsWare points out below) the whole approach in this answer only applies up to and including 2.3.x (Gingerbread). As of Honeycomb Bitmap data is allocated in the VM heap.]

Bitmap data is not allocated in the VM heap. There is a reference to it in the VM heap (which is small), but the actual data is allocated in the Native heap by the underlying Skia graphics library.

Unfortunately, while the definition of BitmapFactory.decode...() says that it returns null if the image data could not be decoded, the Skia implementation (or rather the JNI glue between the Java code and Skia) logs the message you’re seeing ("VM won't let us allocate xxxx bytes") and then throws an OutOfMemory exception with the misleading message "bitmap size exceeds VM budget".

The issue is not in the VM heap but is rather in the Native heap. The Natïve heap is shared between running applications, so the amount of free space depends on what other applications are running and their bitmap usage. But, given that BitmapFactory will not return, you need a way to figure out if the call is going to succeed before you make it.

There are routines to monitor the size of the Native heap (see the Debug class getNative methods). However, I have found that getNativeHeapFreeSize() and getNativeHeapSize() are not reliable. So in one of my applications that dynamically creates a large number of bitmaps I do the following.

The Native heap size varies by platform. So at startup, we check the maximum allowed VM heap size to determine the maximum allowed Native heap size. [The magic numbers were determined by testing on 2.1 and 2.2, and may be different on other API levels.]

long mMaxVmHeap     = Runtime.getRuntime().maxMemory()/1024;
long mMaxNativeHeap = 16*1024;
if (mMaxVmHeap == 16*1024)
     mMaxNativeHeap = 16*1024;
else if (mMaxVmHeap == 24*1024)
     mMaxNativeHeap = 24*1024;
else
    Log.w(TAG, "Unrecognized VM heap size = " + mMaxVmHeap);

Then each time we need to call BitmapFactory we precede the call by a check of the form.

long sizeReqd        = bitmapWidth * bitmapHeight * targetBpp  / 8;
long allocNativeHeap = Debug.getNativeHeapAllocatedSize();
if ((sizeReqd + allocNativeHeap + heapPad) >= mMaxNativeHeap)
{
    // Do not call BitmapFactory…
}

Note that the heapPad is a magic number to allow for the fact that a) the reporting of Native heap size is "soft" and b) we want to leave some space in the Native heap for other applications. We are running with a 3*1024*1024 (ie 3Mbytes) pad currently.

Torid
  • 4,176
  • 1
  • 28
  • 29
  • A very interesting and practical approach. – BonanzaDriver May 26 '11 at 05:28
  • Interesting post. Do you got any proof/sources for this one?: "The Natïve heap is shared between running applications, so the amount of free space depends on what other applications are running and their bitmap usage." – mibollma Jul 08 '11 at 17:35
  • That was based on reading the Skia source, and in particular looking at how it handles bitmap.recycle(). But in retrospect, while I don't see any per-application references / handling in that code (which is what led me to that statement), I now think that's too strong / wrong. The native heap will be allocated per-application, relying on the VM's normal per-process memory handling, and therefore the Skia code does not have to worry about per-application/process issues. – Torid Jul 08 '11 at 18:00
  • @Torid Where you do your If to check if there is enough space to create the bitmap. I don't see how you have the bitmaps width & height before you've decoded it? Also what is targetBpp pls? – Blundell Jul 26 '11 at 20:29
  • You can use the inJustDecodeBounds option in http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html to get the bounds before you decode. – Torid Jul 29 '11 at 19:10
  • 1
    One way to get bpp is to use getWindow().getAttributes().format to get the PixelFormat (http://developer.android.com/reference/android/graphics/PixelFormat.html). From that you can map to bpp (eg OPAQUE is 16pp) – Torid Jul 29 '11 at 19:15
  • 5
    "Bitmap data is not allocated in the VM heap" -- it is allocated on the VM heap as of Honeycomb. – CommonsWare Dec 02 '11 at 23:25
  • Yes. As of Honeycomb (v3.0), bitmap data is allocated on the VM heap. So all of the above only applies to Gingerbread (v2.3.x) and before. – Torid Mar 20 '12 at 16:09
  • Don't use the values from this code. I'm measuring my allocated heap on a gingerbread device and it's around 30568336 bytes. Figure your own reasonable values out. Also, some of the lines of code at the beginning are a tad redundant. However, the concept is priceless. – Thomas Dignan Nov 15 '12 at 23:30
  • @CommonsWare - sir i am currently facing an issue while converting byte data to a bitmap as it crashes with out of memory, in the method onPictureTaken of a surfaceview , how do i solve that ? please assist, i have gone through cwac-camera,but not implemented it . – Rat-a-tat-a-tat Ratatouille Sep 15 '14 at 05:47
2

1.6 MB of memory seems like a lot but it could be the case that the memory is so badly fragmented that it can't allocate such big block of memory in one go (still this does sound very strange).

One common cause of OOM while using image resources is when one is decompressing JPG, PNG, GIF images with really high resolutions. You need to bear in mind that all these formats are pretty well compressed and take up very little space but once you load the images to the phone, the memory they're going to use is something like width * height * 4 bytes. Also, when decompression kicks in, a few other auxiliary data structures need to be loaded for the decoding step.

rui
  • 11,015
  • 7
  • 46
  • 64
2

It seems like the issues given in Torid's answer have been resolved in the more recent versions of Android.

However, if you are using an image cache (a specialized one or even just a regular HashMap), it is pretty easy to get this error by creating a memory leak.

In my experience, if you inadvertently hold on to your Bitmap references and create a memory leak, OP's error (an referring to the BitmapFactory and native methods) is the one that will crash your app (up to ICS - 14 and +?)

To avoid this, make your you "let go" of your Bitmaps. This means using SoftReferences in the final tier of your cache, so that Bitmaps can get garbage collected out of it. This should work, but if you are still getting crashes, you can try to explicitly mark certain Bitmaps for collection by using bitmap.recycle(), just remember to never return a bitmap for use in your app if bitmap.isRecycled().

As an aside, LinkedHashMaps are a great tool for easily implementing pretty good cache structures, especially if you combine hard and soft references like in this example (starting line 308)... but using hard references is also how you can get yourself into memory leak situations if you mess up.

Community
  • 1
  • 1
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
0

Although usually it doesnt make sense to catch an Error because usually they are thrown only by the vm but in this particular case the Error is thrown by the jni glue code thus it is very simple to handle cases where you could not load the image: just catch the OutOfMemoryError.

Zsolt Safrany
  • 13,290
  • 6
  • 50
  • 62
0

Although this is a fairly high level answer, the problem for me turned out to be using hardware acceleration on all of my views. Most of my views have custom Bitmap manipulation, which I figured to be the source of the large native heap size, but in fact when disabling hardware acceleration the native heap usage was cut down by a factor of 4.

It seems as though hardware acceleration will do all kinds of caching on your views, creating bitmaps of its own, and since all bitmaps share the native heap, the allocation size can grow pretty dramatically.

Josh Bothun
  • 1,324
  • 9
  • 9