处理软引用和弱引用时遇到问题.代码有一个标志,可以在软引用和弱引用之间切换所有逻辑.虽然弱引用似乎都工作正常,使用软引用我一直得到OutOfMemoryError.这种情况发生在MacOSX上的JDK7和JDK6以及Debian上的IcedTea6.然而,与JDK7 G1收集器的设置,我发现与软引用的工作,其他的一切我试过(串行/并行GC,-client/-server等)失败抛出异常.
代码有点大,但我试图在保留问题的同时尽量缩小代码.
我在其上面留下了一个大量的评论,更详细地描述了如何重现这个问题.
/*
*
* Leakling.java
*
*
* Issue:
*
*
* This code throws OutOfMemoryError when using soft references, whereas weak references
* work ok. Moreover, with JDK7 G1 garbage collector soft references work as well. Other
* collectors seem to fail. Code was tested with MacOSX 10.8.2 JDKs 1.7.0_10-b18 and
* 1.6.0_37-b06-434-11M3909, with Debian 6.0 IcedTea6 1.8.13.
* Various command line options including -Xmx, -client/-server, -XX:+UseParallelOldGC,
* -XX:+UseSerialGC were tested.
*
*
* Examples:
* …Run Code Online (Sandbox Code Playgroud)