???????????????л???????????“??????á? ?????á? ?????á???????”??
????A???????????new??????????????
????Q?????????????
????A?????WeakReference???????????????????...
????Q???????????????Щ???ü????????????
????A??...
?????浽???????????????????????????????Σ??????????????????Щ?????????′?????????????????????????Ц??????
?????????????oracle???????????????api???
????java.lang.ref
????Class Reference<T>
????java.lang.Object
????java.lang.ref.Reference<T>
????Direct Known Subclasses:
????PhantomReference?? SoftReference?? WeakReference
????Abstract base class for reference objects. This class defines the operations common to all reference objects. Because reference objects are implemented in close cooperation with the garbage collector?? this class may not be subclassed directly.
????Since:
????1.2
???????????JDK 1.2?????汾?У?????????????κα??????????????????????????????????????ж???????????reachable??????????????????????JDK 1.2?汾??????????????÷??4????????????????????????????????????????4??????????????????????á??????á??????ú??????á?
????Strong Reference:
?????????????????????á??????????????????????????????????????κζ?????????????????GC ??к???????????????Java???????????OutOfMemoryError???????????????????????????????????????????????治???????
??????????????????????????????java?е?Reference???????в????????? StrongReference???????????д?????Strong Reference ??stackoverflow??????????????????????
????SoftReference??
????Soft reference objects?? which are cleared at the discretion of the garbage collector in response to memory demand. Soft references are most often used to implement memory-sensitive caches.Suppose that the garbage collector determines at a certain point in time that an object is softly reachable. At that time it may choose to clear atomically all soft references to that object and all soft references to any other softly-reachable objects from which that object is reachable through a chain of strong references. At the same time or at some later time it will enqueue those newly-cleared soft references that are registered with reference queues.
????All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError. Otherwise no constraints are placed upon the time at which a soft reference will be cleared or the order in which a set of such references to different objects will be cleared. Virtual machine implementations are?? however?? encouraged to bias against clearing recently-created or recently-used soft references.
????Direct instances of this class may be used to implement simple caches; this class or derived subclasses may also be used in larger data structures to implement more sophisticated caches. As long as the referent of a soft reference is strongly reachable?? that is?? is actually in use?? the soft reference will not be cleared.Thus a sophisticated cache can?? for example?? prevent its most recently used entries from being discarded by keeping strong referents to those entries?? leaving the remaining entries to be discarded at the discretion of the garbage collector.
??????????????б???????
????· ??????????????????????
???????  ????δ?????治?????????????????????????????????????????????????????
????· ?????ó?????????????檔
??????????????????????滺?棨??????DiskLruCache????
????· ????????????????softly-reachable?????????JVM???OutOfMemoryError????????????????????
?????????
????1.??SoftReference?????滺?????????
????2.?????????????????????????softly-reachable????????????????
????· SoftReference???????????????????;SoftReference??????????????????????????????????????????檔
????· ???????????????????strongly reachabl?? ???????????????y??????????
??????????????????????????Test?????????????????????????????
@Test
public void softReference() {
//???????????????
Object referent = new Object();
SoftReference<Object> softRerference = new SoftReference<Object>(referent);
assertSame(referent?? softRerference.get());//referent '????'
System.gc();
assertNotNull(softRerference.get());
assertNotNull(referent);
referent = null;//referent '????'
System.gc();
assertNotNull(softRerference.get()); //'????'????????????????
}
????WeakReference??
????Weak reference objects?? which do not prevent their referents from being made finalizable?? finalized?? and then reclaimed. Weak references are most often used to implement canonicalizing mappings.Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references. At the same time it will declare all of the formerly weakly-reachable objects to be finalizable. At the same time or at some later time it will enqueue those newly-cleared weak references that are registered with reference queues.
????· ???????????????????????????????
????· ?????ò?????????????????finalizable??finalized????reclaimed
????· ?????ó????????canonicalizing mappings??
????· ????canonicalizing mappings??????????????μ?????????????http://c2.com/cgi/wiki?CanonicalizedMapping
????A "canonicalized" mapping is where you keep one instance of the object in question in memory and all others look up that particular instance via pointers or somesuch mechanism. This is where weaks references can help.
????The short answer is that Weak Reference objects can be used to create pointers to objects in your system while still allowing those objects to be reclaimed by the garbage-collector once they pass out of scope.
???????????????????????и??????????????????????????????????????????????????????У??????????????????????????????????????????????????????檔?????????????????ó?????????????????????κ?Java?????????????á???????????????????????????????????????????Щ?????????????á??????????????????????????????????????????????????????????????????????????????????????????????а??????????????п????????JVM?????????檔