Search This Blog

Friday, June 06, 2008

Soft references, Weak References, Phantom references in Java !!!

The Reference API in Java can leave any sane person fully confused !!!..I tried to make sense of what the different types of references are and what purpose do they solve.I am still not crystal clear about the usability of all these references, but I hope I will understand someday.





ReferentAn object that is softly, weakly, or phantomly referenced from inside a SoftReference, WeakReference, or PhantomReference object, respectively.





Soft references:


The garbage collector might or might not reclaim a softly reachable object depending on how recently the object was created or accessed, but is required to clear all soft references before throwing an OutOfMemoryError.Weakly reachable objects are finalized some time after their weak references have been cleared. The only real difference between a soft reference and a weak reference is that the garbage collector uses algorithms to decide whether or not to reclaim a softly reachable object, but always reclaims a weakly reachable object.


Phantomly reachable objects are objects that have been finalized, but not reclaimed.


The garbage collector will never clear a phantom reference. All phantom references must be explicitly cleared by the program.An excellent article explaining GC in Java is given athttp://www.artima.com/insidejvm/ed2/gc17.html

No comments: