Marton_hun
Marton_hun

Reputation: 584

Is it needed to call Bitmap.recycle() after used (in Android)?

According to Android Reference Document of Bitmap.recycle():

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. 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, many books I read suggest to free memory by calling Bitmap.recycle() once make sure no longer need it.

It make me confused: Is it needed to call Bitmap.recycle() after used?

Upvotes: 19

Views: 17675

Answers (4)

yshahak
yshahak

Reputation: 5096

In my experience, we run a heavy Bitmap compression in production code, and without calling recycle() we run into many OOM Exceptions in old Lollypop devices, and after adding it to the code, the number of OOM reduced significantly.

Upvotes: 2

android developer
android developer

Reputation: 116352

It depends.

If you run your app on Android 3.0 and above, it's not needed as the GC will take care of it perfectly.

However, if you run your app on older versions, since bitmaps don't get monitored well by the GC (it thinks they are the size of a reference), you could get OOM, as shown on Google IO lecture here.

In any case, it's still recommended to call recycle as soon as you are sure you don't need the bitmap anymore. It's good even for new android versions, since it lowers the work needed for automatic memory management...

In fact, I remember I've asked a similar question here.

Also, if you need extra control of bitmaps using JNI, check out this post.

So, in short, the answer is that it's not needed anymore, but still recommended.


EDIT: Ever since Android 8.0, Bitmaps are stored in native memory, so it's harder to reach OOM. In fact, it's technically impossible, as you will get into other issues instead. More information about this can be found here.

Upvotes: 21

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Prior Android 3.0 Bitmaps allocs native memory to store it's pixels, and the recycle() calls delete in that region.

Even with that the GC ins't guaranteed to free up that memory if there's still any references for it.

But this call looks like that helps GC to work better, I developed an app that does extensive usage of memory and running in newer devices calling that or not the app run nearly the same (for older it really improves some performance).

Upvotes: 0

JanBo
JanBo

Reputation: 2933

It is not necessary, but is highly recommended! It will speed up the memory freeing process and will save you of torture with Out Of Memory exception.

I would say its mandatory if you are going to do any serious an memory extensive work with Bitmaps.

Upvotes: 1

Related Questions