title: Why does a hashmap clear() free more memory than a pointer reassignment
body: <p>I have an old and new map that contain hundred thousand entries:</p>
  
  <pre><code>Map&lt;State, CostAndIndex&gt; oldMap = new LinkedHashMap&lt;State, CostAndIndex&gt;();
  Map&lt;State, CostAndIndex&gt; newMap = new LinkedHashMap&lt;State, CostAndIndex&gt;();
  </code></pre>
  
  <p>At the end of each iteration of a loop, I was simply doing this:</p>
  
  <pre><code>oldMap = newMap;
  </code></pre>
  
  <p>With that approach I was running out of memory after a while, even when calling System.gc(); after the reassignment.</p>
  
  <p>Then I added a clear before the reassignment and I no longer run out of memory. </p>
  
  <pre><code>oldMap.clear();
  oldMap = newMap;
  </code></pre>
  
  <p>My question: Why does it change anything?  Doesn't a pointer reassignment tell Java that the map and its content are no longer needed and it can clear the data and reuse the space for whatever purpose?</p>
  
  <p>Note: This is running Java HotSpot 1.7.  With Java HotSpot 1.6, I run out of memory much more quickly with the first approach, which isn't clear to me why either.</p>