Horrendous performance & large heap footprint of Java 8 constructor reference?
<p>I just had a rather unpleasant experience in our production environment, causing <code>OutOfMemoryErrors: heapspace..</code></p>

<p>I traced the issue to my use of <code>ArrayList::new</code> in a function.</p>

<p>To verify that this is actually performing worse than normal creation via a declared constructor (<code>t -&gt; new ArrayList&lt;&gt;()</code>), I wrote the following small method:</p>

<pre><code>public class TestMain {
  public static void main(String[] args) {
    boolean newMethod = false;
    Map&lt;Integer,List&lt;Integer&gt;&gt; map = new HashMap&lt;&gt;();
    int index = 0;

    while(true){
      if (newMethod) {
        map.computeIfAbsent(index, ArrayList::new).add(index);
     } else {
        map.computeIfAbsent(index, i-&gt;new ArrayList&lt;&gt;()).add(index);
      }
      if (index++ % 100 == 0) {
        System.out.println("Reached index "+index);
      }
    }
  }
}
</code></pre>

<p>Running the method with <code>newMethod=true;</code> will cause the method to fail with <code>OutOfMemoryError</code> just after index hits 30k. With <code>newMethod=false;</code> the program does not fail, but keeps pounding away until killed (index easily reaches 1.5 milion).</p>

<p>Why does <code>ArrayList::new</code> create so many <code>Object[]</code> elements on the heap that it causes <code>OutOfMemoryError</code> so fast?</p>

<p>(By the way - it also happens when the collection type is <code>HashSet</code>.)</p>