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 ブロックを元に戻すと、プログラムが永遠に正常に動作しているように見え、「Referent still alive.」と出力されることです。疲れて殺すまでセリフ。

$ 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

このすべてで何が欠けていますか?

4

1 に答える 1

4

まったく奇妙なことではありません。割り当てが失敗し、例外がスローされています。例外をキャッチするということは、プログラムを続行できるということです。例外をキャッチしないということは、例外を処理できず、プログラムが終了するということです。

あなたがやっているあなたのwhileループの中で:

        byte[] x = ref.get();

これにより、ソフト参照から新しいストロング参照が作成されます。それを行うとすぐに、収集の資格がなくなります。新しい割り当てを行う前に、その強力な参照をクリアしません。

x = nullテストを行った後に実行してください。

于 2014-01-03T10:53:38.450 に答える