Rich
Rich

Reputation: 36806

Questions about BitmapFactory.Options in Android

I've been reading about performance issues decoding Bitmaps and have received the OutOfMemoryError "Bitmap exceeds VM budget" issue when I would expect to not be out of memory and read online that this is a common problem referred to as a bug in the framework by many devs.

Typically, I get the error mentioned above on my second pass. Let's say I load a somewhat sizable Bitmap successfully and then recycle it and even set the ref to null. When I call this same method a second time that does the work of loading my Bitmap(s) into memory...crash.

I've read on previous posts here and elsewhere online that there are a few things that can be done with BitmapFactory.Options (for instance, providing an input buffer explicitly). But, before proceeding blindly in using what I've found, I'm hoping someone with more knowledge of these classes can shed a little more light.

  1. What is the difference between providing a byte array to BitmapFactory.Options.inTempStorage explicitly and what the BitmapFactory would be doing without it during the encoding process? Why would/should this help with the OutOfMemoryError? Is it providing a smaller buffer than the default and therefore having more opportunities to clean up before running out of memory?

  2. What is specifically going on when I call Bitmap.recycle(), and why is it different from setting the Bitmap to null? Note: doing either or both has yet to make a difference in whether or not (and when) I get OutOfMemory errors.

  3. Is there ever a time that I should explicitly invite the GC into the picture with these kinds of issues? I've always operated (in both Java and .Net) under the assumption that the GC, for the most part, should be trusted to decide where and when it does its thing because there are more consequences to a collection than just freeing up memory (and I'm personally not well versed in them enough to make that decision).

Update:

This video answered most of my questions:

Google I/O 2011: Memory management for Android Apps

http://www.youtube.com/watch?v=_CruQY55HOk

Upvotes: 1

Views: 2246

Answers (1)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

I found this could be interesting: http://osdir.com/ml/AndroidDevelopers/2009-08/msg01230.html. Have you tried a System.gc() to confirm it comes from a problem of the native heap not being cleaned up properly?

Upvotes: 1

Related Questions