安卓:位图,SoftReferences和奥姆斯? [英] Android: Bitmaps, SoftReferences, and OOMs?

查看:116
本文介绍了安卓:位图,SoftReferences和奥姆斯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的观点在垂直的LinearLayout。每个视图生成并绘制一个位图,当滚动到。出于性能的考虑,我宁可不要每次的OnDraw()被调用生成位图,但对于内存的原因,我不能保持对位图硬引用。我可以对我应该采取的策略使用的建议。

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.

我已经尝试过的明显的路径:生成位图,然后用SoftReference包装它。此失败的原因有两个。 1.参考文献收集得到更急切地比我期望的那样。 2.我仍然得到奥姆斯!这是令人震惊的,因为没有位图是特别大,所以单一视图不应该引起OOM,让我假设出现OOM,因为违规SoftReference(S)尚未有机会被释放。此外,OOM(根据DDMS视图)发生在我的应用程序具有6MB的分配堆大小,我希望它抛出OOM之前将增长到16MB。

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.

有什么建议?

推荐答案

问题是,位图使用终结,因此,它可能需要几个GC通行证的本机内存实际发布之前。这是我们正在努力做出更好的。

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.

这篇关于安卓:位图,SoftReferences和奥姆斯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆