title: Garbage collection - orphaned LinkedList links
body: <p>Suppose you have references <code>A -&gt; B -&gt; C -&gt; D</code>. When you delete the reference to <code>B</code> from <code>A</code>, you're left with an orphaned chain of Objects <code>B -&gt; C -&gt; D</code>.</p>
  
  <p>Will <code>C</code> and <code>D</code> be garbage collected even though there's no way to get to them (since there's no reference to <code>B</code>)?</p>
  
  <p>I imagine the GC is smart about this and will resolve any such dependencies.</p>
  
  <p>However, I took a look into the <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/LinkedList.java#LinkedList.clear%28%29" rel="noreferrer">source code</a> for the <code>LinkedList</code> class and found something contrary to this belief. I noticed that when a list is <code>clear()</code>ed, all of the references to each link are explicitly set to <code>null</code>, thus making it an <code>O(n)</code> operation. Is there any reason/benefit for doing so?</p>