Help us understand the problem. What is going on with this article?

WeakReferenceの動作確認

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は勝手にキャッシュされるので注意は必要。

ma38su
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした