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

五迷三道 提交于 2019-11-30 17:34:10

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.

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.

Marcos Vasconcelos

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!