它接缝Dalvik的垃圾收集器不尊重SoftReferences并尽快删除它们,就像WeakReferences一样.我还没有100%肯定,但是尽管事实上还有〜3MB的可用内存,但是在我看到在“LogCat”中看到“GC释放bla-bla-bla字节”之后,我的SoftReferences被清除了.
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.
是真的吗SoftReference是否不受尊重?
如何解决这个问题?
@H_404_17@解决方法
没有收到答案后,我决定自己做研究.我已经做了一个简单的测试来对SoftReferences执行GC.
@H_502_21@public class TestSoftReference extends TestCase {
public void testSoftRefsAgainstGc_1() { testGcWithSoftRefs(1); }
public void testSoftRefsAgainstGc_2() { testGcWithSoftRefs(2); }
public void testSoftRefsAgainstGc_3() { testGcWithSoftRefs(3); }
public void testSoftRefsAgainstGc_4() { testGcWithSoftRefs(4); }
public void testSoftRefsAgainstGc_5() { testGcWithSoftRefs(5); }
public void testSoftRefsAgainstGc_6() { testGcWithSoftRefs(6); }
public void testSoftRefsAgainstGc_7() { testGcWithSoftRefs(7); }
private static final int SR_COUNT = 1000;
private void testGcWithSoftRefs(final int gc_count) {
/* "Integer(i)" is a referrent. It is important to have it referenced
* only from the SoftReference and from nothing else. */
final ArrayList<SoftReference<Integer>> list = new ArrayList<SoftReference<Integer>>(SR_COUNT);
for (int i = 0; i < SR_COUNT; ++i) {
list.add(new SoftReference<Integer>(new Integer(i)));
}
/* Test */
for (int i = 0; i < gc_count; ++i) {
System.gc();
try {
Thread.sleep(200);
} catch (final InterruptedException e) {
}
}
/* Check */
int dead = 0;
for (final SoftReference<Integer> ref : list) {
if (ref.get() == null) {
++dead;
}
}
assertEquals(0,dead);
}
}
这个想法是,我每次运行相同的代码,每次在SoftReferences上增加压力(通过运行更多的GC通行证).
结果非常有趣:除了一个,所有运行通过只是罚款!
@H_502_21@ On Android 1.5 device: testSoftRefsAgainstGc_1() Failed! AssertionFailedError: expected:0 but was:499 testSoftRefsAgainstGc_2() passed testSoftRefsAgainstGc_3() passed testSoftRefsAgainstGc_4() passed testSoftRefsAgainstGc_5() passed testSoftRefsAgainstGc_6() passed testSoftRefsAgainstGc_7() passed On Android 1.6 device: testSoftRefsAgainstGc_1() passed testSoftRefsAgainstGc_2() Failed! AssertionFailedError: expected:0 but was:499 testSoftRefsAgainstGc_3() passed testSoftRefsAgainstGc_4() passed testSoftRefsAgainstGc_5() passed testSoftRefsAgainstGc_6() passed testSoftRefsAgainstGc_7() passed On Android 2.2 device: All pass.这些测试结果是稳定的.我尝试了很多次,每次都是一样的.所以我相信垃圾回收器确实是一个bug.
结论
所以,我们从中学到什么…在你的代码中使用SoftReferences对于Android 1.5-1.6设备来说是毫无意义的.对于这些设备,您将无法获得期望的行为.不过我并没有尝试2.1.
@H_404_17@ @H_404_17@