Figure 7-7. Memory allocated by indefinite reference
The object being cached occupies 512 bytes. On the left, that's all the memory consumed
(absent the memory for the instance variable pointing to the object). On the right, the object
is being cached inside a SoftReference object, which adds 40 additional bytes of memory
consumption. Indefinite references are just like any other object: they consume memory, and
other things (the cachedValue variable on the righthand side of the diagram) reference them
So the first impact on the garbage collector is that indefinite references cause the application
to use more memory. A bigger impact on the garbage collector is that it takes at least two GC
cycles for the indefinite reference object to be reclaimed by the garbage collector.
Figure 7-8 shows what happens when a referent is no longer strongly referenced (i.e., the
lastViewed variable has been set to null ). If there are no references to the StockHistory
object, it is freed during the next GC. So the left side of the diagram now consumes 0 bytes.