Allows you to parse Javadoc for WeakReference:
Assume that the garbage collector determines at some point in time when the object is poorly accessible.
At this time, it will atomize all weak links to this object and all weak links to any other unreachable objects from which this object is reached through a chain of strong and soft Recommendations.
, .
, .
, :
, WeakReference.get()
null
, , WeakReference.enqueue
true
ReferenceQueue.poll
null
.
. https://community.oracle.com/blogs/enicholas/2006/05/04/understanding-weak-references.
WeakReference null, , , WeakReference . , - : WeakHashMap, , deadWeakReferences.
ReferenceQueue . ReferenceQueueinto , , , , . ReferenceQueue .
, :
public static class A {
}
public static void main(String[] args) throws Exception{
A a = new A();
ReferenceQueue<A> rq = new ReferenceQueue<A>();
WeakReference<A> aref = new WeakReference<A>(a, rq);
a = null;
System.out.println( "0: " + aref + " : " + aref.get() + " : " + aref.isEnqueued() + " " + rq.poll() );
Thread.sleep(1000);
System.out.println("Running GC.");
Runtime.getRuntime().gc();
System.out.println("GC ran.");
System.out.println( "1: " + aref + " : " + aref.get() + " " + aref.isEnqueued() + " " + rq.poll() );
Thread.sleep(1000);
System.out.println( "2: " + aref + " : " + aref.get() + " " + aref.isEnqueued() + " " + rq.poll() );
}