title: JProfiler: Trying to find memory leak
body: <p>My application needs about 10 GByte of RAM for specific input, where for regular inputs roughly 1 GByte is enough. Closer analysis with JProfiler reveals that (after GC) quite a lot of memory is used by standard classes from <code>java.util.*</code>:</p>
  
  <p><code>LinkedHashMap$Entry</code>, <code>HashMap$Entry[]</code>, <code>LinkedHashMap</code>, <code>HashMap$KeySet</code>, <code>HashMap$EntrySet</code>, <code>LinkedHashSet</code>, <code>TreeMap$Entry</code>, and <code>TreeMap</code> (in this order), and related classes. The following entry is of a class in my own code, where the number of instances and the amount of used memory seems very reasonable.</p>
  
  <p>In detail with a total heap usage of about 900 MByte I see the following <code>Size</code> entries in the <code>All Objects</code> view:</p>
  
  <ul>
  <li><code>LinkedHashMap$Entry</code>: 418 MByte</li>
  <li><code>HashMap$Entry[]</code>: 178 MByte</li>
  <li><code>LinkedHashMap</code>: 124 MByte</li>
  <li><code>HashMap$KeySet</code>: 15 MByte</li>
  </ul>
  
  <p>The memory used by <code>LinkedHashMap</code> seems to be too high, even when considering that every <code>LinkedHashSet</code> is backed by a <code>LinkedHashMap</code>.</p>
  
  <p>I recorded object allocation in JProfiler and observe the <code>Allocation Hot Spots</code> for <code>LinkedHashMap</code>. In there, I see entries that I do not understand:</p>
  
  <ul>
  <li>The third entry shows a hot spot (with 6.5% of the allocated memory) named <code>X.&lt;init&gt;</code> where <code>X</code> is a class in my own code. The constructors of this method do not have anything to do with <code>LinkedHashMap</code>. Following this entry to <code>Thread.run</code> at the end shows a slow decline from 6.5% to 5.8% at <code>Thread.run</code>. What could be the problem with my code in <code>X</code>? Why is it shown here?</li>
  <li>About 8% of allocated memory is shown in the hot spot named <code>java.util.HashSet.iterator</code>. Following this entry along the path with the highest percentage (first entry: 2.8%) I get several methods inside my code until, finally, <code>java.lang.Thread.run</code> is shown (with 2.8%). What does this mean? As far as I know the <code>Thread.run</code> method does not create instances of <code>LinkedHashMap</code>. What is the connection to the <code>iterator</code> method?</li>
  </ul>
  
  <p>In general, how do I find the code that keeps references to (a lot of) <code>LinkedHashMap</code> objects? Using the Heap Walker I can only see <em>lots</em> of those instances, but do not see any pattern (even when observing the paths to the GC roots). In my experiments all instances seem to be in order.</p>
  
  <p>Possibly important things:</p>
  
  <ul>
  <li>My application constructs a result (for further processing) and, for this construction, the high memory usage is observed. The construction continually creates objects, so waiting for a stable point and then observing every single created <code>LinkedHashMap</code> object is not possible.</li>
  <li>I have good computers available to debug (up to 48 cores and 192 GByte of RAM, possibly even more).</li>
  <li>java version "1.7.0_13" (Java(TM) SE Runtime Environment (build 1.7.0_13-b20), 
  Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode))</li>
  <li>JProfiler 7.2.2 (Build 7157), licensed</li>
  </ul>