How to find out how much free heap size is available for bitmap for Android 2.*?
Asked Answered
A

2

8

Our application uses a lot of bitmaps. It works fine for example on G1, XOOM. But on HTC Desire there is an OutOfMemory error. In code we use try/catch(OutOfMemoryError e) and all devices(except Desire) throw exception, but HTC just kills the application without OOM exception. We restricted the memory for bitmaps to 12 Mb and it seemed that this solution fixed the problem, but customer still has problem on HTC Desire HD. There is OOM even with 12 Mb restriction. Here are the logs:

06-07 12:03:43.978 E/dalvikvm-heap( 29616):1140128-byte external allocation too large for this process. 
06-07 12:03:43.978 E/dalvikvm( 29616):Out of memory: Heap Size=12311KB, Allocated=9420KB, Bitmap Size=12139KB, Limit=21884KB
06-07 12:03:43.978 E/dalvikvm( 29616):Trim info: Footprint=15751KB, Allowed Footprint=15751KB, Trimmed=3440KB
06-07 12:03:43.978 E/GraphicsJNI( 29616):VM won't let us allocate 1140128 bytes

AFAIK there are different heap size limitations for devices(G1: 16mb, Droid: 24 mb, Xoom 48 mb). In my opinion system should give at least 16 mb, but we have OOM with 12mb. My question is: How to find out how many free heap size available for bitmap for Android 2.*? Or please advice how to avoid such problem in other ways. FYI we can't use less bitmaps, especially when it works fine on other devices. Thanks in advance for any help!

Alves answered 7/6, 2011 at 19:52 Comment(0)
K
2

You can try

Runtime.getRuntime().maxMemory();

or Activity's method

getMemoryInfo(ActivityManager.MemoryInfo;

Besides, you can override Activity's

onLowMemory();

method, in which you can handle what will happen when the Activity get's notified of the memory issue, before being shut down. And you should also check this answer from an Android dev on Android/Linux memory.

Kannan answered 7/6, 2011 at 20:0 Comment(2)
AFAIK bitmaps are allocated on the external heap. Dunno if those methods will give you info of the "normal" and external heap of an application. Also see: this post.Thorvaldsen
They are indeed allocated on native heap. But at least for pre-Honeycomb versions of Bitmap the native bitmap code makes sure to reserve the same amount of Java heap (as an array) in case the pixel data is being requested from Java code (through setPixel, getPixel etc.). In that case JNI copies over the native pixel data to the Java realm, therefore it is necessary to also check the available Java heap space.Benumb
H
0

This OutOfMemory exception comes from a memory leak not from the file itself. Are you overriding the activity lifecycle methods like onStop, onResume, onDestroy?

Hap answered 22/6, 2012 at 17:37 Comment(1)
Not how it works at all. If you try and load a 8MP image you will cause an OOM. It doesn't have to be leaky code.Luncheon

© 2022 - 2024 — McMap. All rights reserved.