When (if at all) should I use Bitmap.recycle()?

According to Android Developers site, the Bitmap.recycle() method definition is:

Free the native object associated with this bitmap, and clear the reference to the pixel data

I’ve developed some applications which are creating / decoding a lot of bitmaps, and put the result bitmap objects to ImageViews. Sometimes I’ve got the famous exceptions such as:

bitmap size excceded vm budget


out of memory error

Also I’m sure I don’t have any memory leaks that can cause that.

After a lot of searches, I discoverd the “recycle” method, and used it to free the bitmap’s native memory when no longer needed. It seems like it helped a lot.

I’m asking if that’s something I’m supposed to do on this situation, because
I know the system is doing this anyway without calling it explicitly (is it? maybe I’m wrong).

Should I use this method in situations like this?

In what situations should I use this method?

Should I use this method at all?

thanks in advance.


google posted this guide recently, which says:

On Android 2.3.3 (API level 10) and lower, using recycle() is recommended. If you’re displaying large amounts of bitmap data in your app, you’re likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible.

Solution #1:

in what situations should I use this method?

The Bitmaps are GC’ed by GC whenever it decides.But in some situations it may get delayed.
And always remember thumb rule in java (Maybe it applies to othe P.L also).The speed of recycling objects by GC may not be same as speed of creating objects.So sometimes the GC is slow to in recycling.

so recycle() means If you want to free memory ASAP you should call recycle()

should I use this method at all??

This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.But if you are facing the issues like bitmap size exceeded vm budget or out of memory error then you need to use this.

Respondent: Vipul

Solution #2:

I do use it in operations where i know that bitmap is not going to be used anymore.

public static Bitmap getMaskedContactImage (Context context, Bitmap contactImageBitmap, int maskToBeApplied) {  
    Bitmap mask = BitmapFactory.decodeResource(context.getResources(), maskToBeApplied);
    Bitmap output = Bitmap.createBitmap(mask.getWidth(),mask.getHeight(), Config.ARGB_8888);
    final Rect finalRect = new Rect(0, 0, contactImageBitmap.getWidth(), contactImageBitmap.getHeight());
    final Rect originRect = new Rect(0, 0, mask.getWidth(), mask.getHeight());
    Canvas canvas = new Canvas(output);

    Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    xferPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    canvas.drawBitmap(contactImageBitmap, finalRect, originRect, null);
    canvas.drawBitmap(mask, originRect, originRect, xferPaint);


    return output;

In places like that one, im sure im not going to use the mask or the contactImage.

I found a really good resource for Bitmap processing that can be helpfull Displaying bitmaps.


Respondent: Goofyahead

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.