Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

and

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.

UPDATE:

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.

like image 616
Tal Kanel Avatar asked Jun 26 '12 07:06

Tal Kanel


1 Answers

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.

like image 193
Vipul Avatar answered Nov 02 '22 23:11

Vipul