Android: Bitmaps, SoftReferences, and OOMs?
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?
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.
精彩评论