由于没有得到答案,我决定自己做一项研究。我进行了一项简单的测试,以检验GC对软引用的处理能力。
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) {
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)));
}
for (int i = 0; i < gc_count; ++i) {
System.gc();
try {
Thread.sleep(200);
} catch (final InterruptedException e) {
}
}
int dead = 0;
for (final SoftReference<Integer> ref : list) {
if (ref.get() == null) {
++dead;
}
}
assertEquals(0, dead);
}
}
这个想法是我多次运行相同的代码,每次增加对SoftReferences的压力(通过运行更多的GC passes)。
结果非常有趣:除了一个之外,所有运行都通过了!
在Android 1.5设备上:
testSoftRefsAgainstGc_1() 失败!AssertionFailedError: expected:0 but was:499
testSoftRefsAgainstGc_2() 通过
testSoftRefsAgainstGc_3() 通过
testSoftRefsAgainstGc_4() 通过
testSoftRefsAgainstGc_5() 通过
testSoftRefsAgainstGc_6() 通过
testSoftRefsAgainstGc_7() 通过
在Android 1.6设备上:
testSoftRefsAgainstGc_1() 通过
testSoftRefsAgainstGc_2() 失败!AssertionFailedError: expected:0 but was:499
testSoftRefsAgainstGc_3() 通过
testSoftRefsAgainstGc_4() 通过
testSoftRefsAgainstGc_5() 通过
testSoftRefsAgainstGc_6() 通过
testSoftRefsAgainstGc_7() 通过
在Android 2.2设备上:
全部通过。
这些测试结果是稳定的。我尝试了很多次,每次都是一样的。所以我相信这确实是垃圾收集器中的一个bug。
结论是... 在Android 1.5-1.6设备中,使用SoftReferences在你的代码中是没有意义的。对于这些设备,你将无法得到你期望的行为。我没有尝试过2.1,不过。