Java Reference
In-Depth Information
ory and practice: Plugging memory leaks with weak references” ( ht-
tp://www.ibm.com/developerworks/java/library/j-jtp11225/
index.html ) tutorials to be helpful in understanding the Reference API.
Basic Terminology
Whenanapplicationruns,itsexecutionrevealsa root set of references ,acollectionof
localvariables,parameters,classfields,andinstancefieldsthatcurrentlyexistandthat
contain(possiblynull)referencestoobjects.Thisrootsetchangesovertimeastheap-
plication runs. For example, parameters disappear after a method returns.
Manygarbagecollectorsidentifythisrootsetwhentheyrun.Theyusetherootsetto
determineifanobjectis reachable (referenced,alsoknownas live )or unreachable (not
referenced).Thegarbagecollectorcannotcollectreachableobjects.Instead,itcanonly
collect objects that, starting from the root set of references, cannot be reached.
Note Reachable objects include objects that are indirectly reachable from root-set
variables, which means objects that are reachable through live objects that are direc-
tlyreachablefromthosevariables.Anobjectthatisunreachablebyanypathfromany
root-set variable is eligible for garbage collection.
BeginningwithJava1.2,reachableobjectsareclassifiedasstronglyreachable,softly
reachable,weaklyreachable,andphantomreachable.Unlikestronglyreachableobjects,
softly, weakly, and phantom reachable objects can be garbage collected.
Going from strongest to weakest, the different levels of reachability reflect the life
cycle of an object. They are defined as follows:
• Anobjectis strongly reachable ifitcanbereachedfromsomethreadwithout
traversingany Reference objects.Anewlycreatedobject(suchastheobject
referencedby d in Double d = new Double(1.0); )isstronglyreach-
able by the thread that created it. (I will discuss threads later in this chapter.)
• An object is softly reachable if it is not strongly reachable but can be reached
by traversing a soft reference (a reference to the object where the reference is
storedina SoftReference object).Thestrongestreferencetothisobjectisa
softreference.Whenthesoftreferencestoasoftlyreachableobjectarecleared,
the object becomes eligible for finalization (discussed in Chapter 2 ) .
• Anobjectis weakly reachable ifitisneitherstronglyreachablenorsoftlyreach-
able,butcanbereachedbytraversinga weak reference (areferencetotheob-
jectwherethereferenceisstoredina WeakReference object).Thestrongest
Search WWH ::




Custom Search