Search code examples
androidmemory-managementbitmapandroid-bitmapbitmapfactory

Proper way to release bitmap memory


I'm developing my first Android app. I have a method that creates two bitmaps and returns a third bitmap which is an overlay of the second bitmap on top of the first bitmap. So basically, I don't need the two bitmaps once the third bitmap is created. I've read some posts and articles about releasing bitmap memory and I'm a bit confused on how to handle it.

Do I have to release the bitmaps myself? If yes, what is the proper way to do so?Are they released when the method is finished? Should I just let the garbage collector release it?

public static Bitmap bitmapResizeOverlay(Context context, Uri selectedImage, int maxWidth,
                                         int maxHeight, @DrawableRes int overlayImageResource) {
    Bitmap selectedBitmap = bitmapResize(context, selectedImage, maxWidth, maxHeight);
    Bitmap overlayBitmap = BitmapFactory.decodeResource(context.getResources(), overlayImageResource);
    return overlayBitmapToBottom (selectedBitmap, overlayBitmap);
}

Solution

  •  selectedBitmap.recycle()

    method is used always in when you want clear the memory occupied with bitmap. The down side of not recycling the bit map could be OOM (out of Memory Exception)