Last active
August 10, 2019 01:31
-
-
Save zencd/32ca56434e067e0b18b3ab3de6b0ef7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WeakReferenceResurrection { | |
public static void main(String[] args) { | |
var que = new ReferenceQueue<Object>(); | |
var weak = new WeakReference<>(new Object(), que); | |
System.out.println("gc..."); | |
System.gc(); | |
Reference polled; | |
while ((polled = que.poll()) == null) { | |
System.out.println("not GC'ed yet"); | |
} | |
System.out.println("ref appeared in queue: " + polled); | |
System.out.println("its referent: " + polled.get()); | |
polled.clear(); | |
} | |
} | |
// Output: | |
// gc... | |
// not GC'ed yet | |
// ref appeared in queue: java.lang.ref.WeakReference@4eec7777 | |
// its referent: null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment