Android:GC 不尊重软引用?
看来 Dalvik 的垃圾收集器不尊重 SoftReferences 并尽快删除它们,就像 WeakReferences 一样。我还不是 100% 确定,但尽管事实上仍然有大约 3MB 的可用内存,但在 LogCat 中看到“GC 释放了 bla-bla-bla 字节”后,我的 SoftReferences 被清除了。
另外,我在此处看到了 Mark Murphy 的评论:
除非它不起作用 Android,至少在 1.5 中 大体时间。我不知道GC是否 SoftReference 错误已得到修复。 SoftReferences 太快被 GC 了 这个错误。
这是真的吗?软引用不受尊重吗?
如何解决这个问题?
It seams that Dalvik's garbage collector doesn't respect SoftReferences and removes them as soon as possible, just like WeakReferences. I'm not 100% sure yet, but despite the fact that there is still ~3MB of free memory my SoftReferences get cleared after I see "GC freed bla-bla-bla bytes" in LogCat.
Also, I saw a comment by Mark Murphy here:
Except that it doesn't work on
Android, at least in the 1.5
timeframe. I have no idea if the GC
SoftReference bugs have been fixed.
SoftReferences get GC'd too soon with
this bug.
Is it true? Are SoftReferences not respected?
How to workaround this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在没有收到答案后,我决定自己研究。我做了一个简单的测试来针对 SoftReferences 执行 GC。
我的想法是,我很少运行相同的代码,每次都会增加软引用的压力(通过运行更多的 GC 遍)。
结果非常有趣:除了一次之外,所有运行都顺利通过!
这些测试结果是稳定的。我已经尝试了很多次,每次都是一样的。所以我相信这确实是垃圾收集器中的一个错误。
结论
因此,我们从中了解到...在代码中使用 SoftReferences 对于 Android 1.5-1.6 设备毫无意义。对于这些设备,您将无法获得您期望的行为。不过,我没有尝试 2.1。
After not receiving an answer I decided to make my own study. I've made a simple test to exercise the GC against SoftReferences.
The idea is that I make few runs of the same code increasing stress on SoftReferences each time (by running more GC passes).
Results are pretty interesting: All runs pass just fine except for one!
These test results are stable. I've tried many times and every time it is the same. So I believe it is indeed a bug in garbage collector.
CONCLUSION
So, what we learn out of this... Using SoftReferences in your code is pointless for Android 1.5-1.6 devices. For these devices you will not get the behavior you expect. I didn't try for 2.1, however.
@JBM我已经在Nexus S(android4.2.2)上尝试过你的TestCase,所有测试都失败了。 android4.2.2上GC对SoftReference更加激进
@JBM I've tried your TestCase on Nexus S (android4.2.2), all tests are failed. GC is more aggressive against SoftReference on android4.2.2
我向 Google 报告了此问题:https://code.google.com/ p/android/issues/detail?id=20015
I reported this issue to Google: https://code.google.com/p/android/issues/detail?id=20015