Java SoftReference 无法保证成功

6
JDK 7文档中关于SoftReference的描述如下:

"在虚拟机抛出OutOfMemoryError之前,所有指向软可达对象的软引用保证已被清除。"

然而,在我的测试程序中,我一直看到OutOfMemoryError(除了下面的“陌生行为”部分)。
// RefObjectTest.java

import java.util.*;
import java.lang.ref.*;

public class RefObjectTest {

    public static void main(String[] args) {

        ArrayList<byte[]> leaks = new ArrayList<>();

        byte[] obj = new byte[10 * 1024 * 1024];

        SoftReference<byte[]> ref = new SoftReference<>(obj);

        // WeakReference is supposed to be eagerly GC'ed, but I see no
        // difference in terms of program behavior: still get OOME.
        //
        // WeakReference<byte[]> ref = new WeakReference<>(obj);

        obj = null;

        while(true) {
            byte[] x = ref.get();
            if(x == null) {
                System.out.println("Referent stands garbage collected!!");
                break;
            } else {
                System.out.println("Referent still alive.");
            }

            // Leak memory in small, 10k increments. If soft reference
            // worked the way it's advertized, then just before the OOME, the 
            // 10MB referent should have been GC'ed to accomodate this small
            // 10K new memory request. But it doesn't appear to work that way!

//          try {
                leaks.add(new byte[10 * 1024]);
//          } catch(OutOfMemoryError e) {
//              System.out.println(ref.get() == null ? "Referent cleared" : 
//                  "Referent still alive");
//          }

            // VERY STRANGE: If I re-instate the above try-catch block, then no OOME is
            // thrown, and the program keeps printing "Referent still alive" lines
            // forever until I finally kill it with a Ctrl+C.

            // Uncommenting gc() tends to delay the OOME in terms of time, 
            // but OOME eventually does happen, and after the same number of
            // iterations of this loop. 
            // 
            // System.gc();
        }
    }
}

以下是输出内容:
$ java -Xmx15m RefObjectTest
Referent still alive.
  ...
Referent still alive.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at RefObjectTest.main(RefObjectTest.java:38)

陌生行为

非常奇怪的是,如果我重新添加try-catch块,程序似乎可以一直正常运行,打印“引用仍然存在”的行,直到我感到疲倦并终止它。

$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
$ 
$ uname -a
Linux ida 3.10.11-100.fc18.x86_64 #1 SMP Mon Sep 9 13:06:31 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

在所有这些中,我错过了什么?
1个回答

4

这并不奇怪,分配失败会抛出异常。通过捕获异常,您表明程序可以继续运行;如果不捕获异常,则表明无法处理该异常,程序将退出。

在 while 循环内部,您正在执行以下操作:

        byte[] x = ref.get();

这是从您的软引用创建一个新的强引用。一旦这样做,它就不再适合收集。在进行新的分配之前,请务必清除该强引用。

在进行测试后,请执行x = null


谢谢您的快速回复!(我甚至在我的测试代码中都搞砸了!) - Harry
开头的 byte[] obj 是另一个强引用,会防止清除软引用。 - Jörn Horstmann
通常情况下你是对的,但是在 while(true) 之前那个会被设置为 null。 - Tim B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接