??????????????????£?
@Test
public void weakReference() {
Object referent = new Object();
WeakReference<Object> weakRerference = new WeakReference<Object>(referent);
assertSame(referent?? weakRerference.get());//referent '????'
System.gc();
assertNotNull(weakRerference.get());
assertNotNull(referent);//referent '????' ????????
referent = null;//?????????referent '?????'
System.gc();
assertNull(weakRerference.get()); //'?????'?????????????????
referent = null;
System.gc();
assertNull(weakRerference.get()); //`?????`
}
????'????'??'????' ??'?????' ??'?????'??Щ???????????????????±?????????????????????oracle???????????Щ?????
????Reachability
????Going from strongest to weakest?? the different levels of reachability reflect the life cycle of an object. They are operationally defined as follows:
????· An object is strongly reachable if it can be reached by some thread without traversing any reference objects. A newly-created object is strongly reachable by the thread that created it.
????· An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference.
????· An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared?? the object becomes eligible for finalization.
????· An object is phantom reachable if it is neither strongly?? softly?? nor weakly reachable?? it has been finalized?? and some phantom reference refers to it.
????Finally?? an object is unreachable?? and therefore eligible for reclamation?? when it is not reachable in any of the above ways.
????Since:
????1.2
????????????Since 1.2 ???????????????Test????????????????ɡ?
????PhantomReference??
????Phantom reference objects?? which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.If the garbage collector determines at a certain point in time that the referent of a phantom reference is phantom reachable?? then at that time or at some later time it will enqueue the reference.
????In order to ensure that a reclaimable object remains so?? the referent of a phantom reference may not be retrieved: The get method of a phantom reference always returns null.
????Unlike soft and weak references?? phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.
????· ?????????????????????????????????κ?????????????κ????????????????????????
????· ??????????????????????????????????????