问题描述
我在垂直 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!这是令人震惊的,因为没有 Bitmap 是特别大的,所以单个视图不应该导致 OOM,让我假设 OOM 发生是因为有问题的 SoftReference(s) 没有被释放的机会.此外,当我的应用程序分配的堆大小为 6mb(根据 DDMS 视图)时,会发生 OOM,我希望它在抛出 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.
这篇关于Android:位图、软引用和 OOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!