Java Reference
In-Depth Information
/* At this point, the big object with id 101 is softly reachable,
because it can be reached only through a soft reference sr.
*/
// Get the reference of referent of soft reference object
BigObject referent = sr.get();
/* At this point, the big object with id 101 again becomes strongly reachable
because referent is a strong reference. It also has a soft reference referring to it.
*/
Figure
11-6
depicts the memory states with all the references after you execute each statement in this snippet of code.
bigObj
id:101
X
BigObject bigObj = new BigObject(101);
bigObj
id:
101
X
sr
SoftReference
Object
X
BigObject bigObj = new BigObject(101);
SoftReference<BigObject> sr = new SoftReference<BigObject>(bigObj);
bigObj
X
id:101
sr
SoftReference
Object
X
BigObject bigObj = new BigObject(101);
SoftReference<BigObject> sr = new SoftReference<BigObject>(bigObj);
bigObj = null;
bigObj
X
id:101
referent
X
sr
SoftReference
Object
X
BigObject bigObj = new BigObject(101);
SoftReference<BigObject> sr = new SoftReference<BigObject>(bigObj);
bigObj = null;
BigObject referent = sr.get( );
Figure 11-6.
Accessing the referent of a reference object