Android: Bitmaps, SoftReferences, and OOMs?

谁说我不能喝 提交于 2019-11-27 15:37:02

问题


I have a series of views in a vertical LinearLayout. Each view generates and draws a Bitmap, when scrolled to. For performance reasons, I would rather not generate the Bitmap each time onDraw() is called, but for memory reasons I can not keep hard references to the Bitmaps. I could use advice on the strategy that I should take.

I already tried the obvious route of: generating the Bitmap, and then wrapping it with a SoftReference. This failed for two reasons. 1. The references get collected far more eagerly than I would expect. 2. I still get OOMs! Which is shocking, because no Bitmap is particularly large, so a single view should not cause the OOM, making me assume that the OOM occurs because the offending SoftReference(s) had not been given a chance to be freed. Also, the OOM occurs when my application is has an allocated heap size of 6mb (according to DDMS view), I would expect it to grow to 16mb before throwing OOM.

Any advice?


回答1:


The problem is that Bitmaps use finalizers, therefore, it might take a couple of GC passes before the native memory is actually released. This is something we are working on making better.



来源:https://stackoverflow.com/questions/4347411/android-bitmaps-softreferences-and-ooms

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