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

4

19

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?

Keek answered 26/8, 2013 at 20:8 Comment(1)
The GC will free up the Bitmap when it's not used, but calling the recycle() will help the GC to check if that part of the memory is free. Calling recycle() when loading and unloading Bitmaps (like in a list or gallery) may be a good practiceJacquez
H
21

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.

Hypophosphite answered 26/8, 2013 at 20:41 Comment(4)
so even if a methods finish, and the BMP file loos reference, 1.it won't free up fully on android, or 2. it will collect it's reference step by step such a way make it slow to recover? like Bitmap->PixelArrayData->Pixel it first Remove the bitmap which has no reference to it, then the pixelArrayData, and in third launch the pixel it selvesLacking
As long as anything still holds a reference to it and can use it, it won't be recycled, just like any other Java object. BTW, I've decided to add a note in my answer about Android 8.0.Hypophosphite
well if it's not defined in long running process as global, like involved class, or and long running like while true loop, it shouldn't be matter, at some point gc should start work, and gather it all, or part of it.... long ago, when i start writing one of my first C# application, i noticed that when i generate Bitmap frame by frame, it take some part of memory over time, then OS clear some of it not all (windows) then again, until it full the ram, but using GC.Collect if i'm not wrong, after each frame or after a period it would remove whole unnecessariesLacking
I think you had a bug on C#, or you just didn't really have a reference to those bitmaps anymore. I don't think it can ditch bitmaps automatically without a reason. Again, if there are no references to the object, the object can be freed. It can be as soon as it's not available, and it can be some time later.Hypophosphite
C
2

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.

Cameron answered 20/12, 2018 at 14:57 Comment(0)
H
1

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.

Hydromagnetics answered 26/8, 2013 at 20:31 Comment(0)
M
0

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).

Munguia answered 26/8, 2013 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.