title: Java Soft References
body: <p>Suppose I have three objects, A, B and C.</p>
  
  <p><code>A</code> refers to <code>B</code> with a soft reference.<br>
  <code>B</code> refers to <code>A</code> with a strong reference.<br>
  <code>B</code> also refers to <code>C</code> with a strong reference.<br>
  <code>C</code> also refers to <code>B</code> with a strong reference.<br> 
  <code>C</code> also refers to <code>A</code> with a strong reference.<br></p>
  
  <p>Let us assume that there are no external references to <code>B</code> or <code>C</code>. If we imagine a situation where the garbage collector decides that if it can, it will try and release memory pointed to by soft references, will <code>B</code> and <code>C</code> be released, leaving <code>A</code>? Or does some sort of circular dependency occur where the back-links from <code>B</code> and <code>C</code> to <code>A</code> somehow prevent garbage collection from happening? My understanding is that the GC should collect soft-references before OutOfMemory is thrown. </p>
  
  <p>This is a highly memory intensive application that models a very large hierarchy, and as such I want the GC to discard "branches" of the hierarchy so long as there are no strong references to them. I can regenerate the branches when necessary, but that is computationally expensive, thus I implemented a cache using soft references. My general strategy is to have soft references pointing downstream (i.e. to sub-nodes of the tree) and strong references pointing back up to the parent nodes. </p>