Android: How Does Bitmap Recycle() Work

Android: How does Bitmap recycle() work?

The first bitmap is not garbage collected when you decode the second one. Garbage Collector will do it later whenever it decides. If you want to free memory ASAP you should call recycle() just before decoding the second bitmap.

If you want to load really big image you should resample it. Here's an example: Strange out of memory issue while loading an image to a Bitmap object.

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


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.

What does Bitmap#recycle() in Android Honeycomb actually DO?

I have discovered that, in Honeycomb onwards, if an ImageView contains a Bitmap that has been recycled, the Bitmap data is still retained in memory until setImageBitmap(null) is called on the ImageView. This may even be the case if setImageResource(...) or setImageDrawable(...) are called (in this case, a very large bitmap was replaced with a fairly small nine-patch, but only when setImageBitmap(null) was called before loading the nine-patch was the memory actually disposed).

How to effectively recycle a Bitmap which is created as per below code?

In this particular case, no, you shouldn't call recycle(); the ImageView will call recycle() when it is done with it. This has been true for a while, ICS did nothing to change this fact.

You need to call recycle() when your code is done with the image. For example if you were applying 10 filters to one image and generating a new Bitmap on each step, you SHOULD call recycle() on the old Bitmap after each step.

That said, you can't have an unlimited number of Bitmaps at the same time, especially large ones. That's when you need to be clever and load/unload dynamically.

Recycle work good or not? in onDestroy()

After marking a bitmap as recyclable, it will only be freed when GC execute.
Here you have more info: Android: Bitmap recycle() how does it work?

But your question is pretty unclear. If you are recycling bitmaps in the onDestroy, it does not make sense because in the onDestroy all views will be freed and the reference for the bitmap too, so it will be GCed in the next execution.

Probably the best option for you is to resample your images. The link I posted have more info.

What does calling bitmap.recycle() on API 11+ do?

Official documentation tells that recycle() now is an advanced call so if you want to free your bitmap you can just write something like bitmap = null and GC will take care of everything else.



Related Topics



Leave a reply



Submit