memory management – Android: "trying to use a recycled bitmap" error with temporary Bitmaps-ThrowExceptions

Exception or error:

My app can load quite large images. In an effort to be memory-conservative, I’m attempting to use a temporary bitmap to load and another for the final image after transformation:

.....
finalBitmap.recycle();
finalBitmap = null;
Bitmap tempBitmap  = BitmapFactory.decodeStream(fin, ...);
finalBitmap = Bitmap.createBitmap(tempBitmap, ....);
imgview.setImageBitmap(finalBitmap);
.....

Now, at this point we’re done with tempBitmap, which was only needed to transport the decoded Bitmap to the transformation step in createBitmap. So:

.....
tempBitmap.recycle();
tempBitmap = null;
.....

And… it crashes with a “trying to use a recycled bitmap” error specifically because of the recycling of tempBitmap. tempBitmap wasn’t displayed and is only used right there.

What’s going wrong here? Should I just use “finalBitmap” throughout and rely on createBitmap to manage it (finalBitmap = Bitmap.createBitmap(finalBitmap , ….))? I fail to see what ongoing dependency on tempBitmap there would be that would cause such a failure.

Edit: Yes, the null assignment seems to result in the appropriate, eventual garbage collection, but I’m mystified as to why recycle() on a temp Bitmap is so problematic in this case. I get the impression that createBitmap() is holding a reference to it but why, and for how long?

How to solve:

Straight from the Android documentation:

Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source, or a copy may
have been made.

It seems that the createBitmap functions have the potential to re-use the bitmap that you provided. If that is the case, then you shouldn’t recycle the temporary bitmap since your final bitmap is using it. One thing you can do is

if(tempBitmap != finalBitmap) {
   tempBitmap.recycle();
}

That should only recycle the tempBitmap when it isn’t the same as the finalBitmap. At least that seems to be what the documentation is implying.

Leave a Reply

Your email address will not be published. Required fields are marked *