0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WeakReferenceの動作確認

Last updated at Posted at 2020-01-01

JavaのWeakReferenceの動作を確認する。

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public class Main {

    static class Entry{
        private final String name;

        public Entry(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }

    public static void main(String[] args) {
        var val = new Entry("Hello");
        var ref = new WeakReference<>(val);

        System.out.println("ref: "+ val + ", weak-ref: "+ ref.get());

        System.out.println("gc");
        System.gc();

        System.out.println("ref: "+ val + ", weak-ref: "+ ref.get());

        System.out.println("e = null");
        val = null;

        System.out.println("gc");
        System.gc();

        System.out.println("ref: "+ val + ", weak-ref: "+ ref.get());
    }
}

実行結果は以下。

ref: Hello, weak-ref: Hello
gc
ref: Hello, weak-ref: Hello
e = null
gc
ref: null, weak-ref: null

ちなみに,
var e = "Hello";
でも同じ結果を期待したのだけれど,GCされずに残った。

ref: Hello, weak-ref: Hello
gc
ref: Hello, weak-ref: Hello
e = null
gc
ref: null, weak-ref: Hello

多分,コード自体が"Hello"を保持しているため,GC対象にならなかった可能性が高い。そこで以下のように明示的にnewすると同じ結果になった。

var e = new String("Hello");

StringでなくIntegerとかで試すときもvalueOfは勝手にキャッシュされるので注意は必要。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?