title: Parallel Infinite Java Streams run out of Memory
body: <p>I'm trying to understand why the following Java program gives an <code>OutOfMemoryError</code>, while the corresponding program without <code>.parallel()</code> doesn't.</p>
  
  <pre class="lang-java prettyprint-override"><code>System.out.println(Stream
      .iterate(1, i -&gt; i+1)
      .parallel()
      .flatMap(n -&gt; Stream.iterate(n, i -&gt; i+n))
      .mapToInt(Integer::intValue)
      .limit(100_000_000)
      .sum()
  );
  </code></pre>
  
  <p>I have two questions:</p>
  
  <ol>
  <li><p>What is the intended output of this program?</p>
  
  <p>Without <code>.parallel()</code> it seems that this simply outputs <code>sum(1+2+3+...)</code> which means that it simply "gets stuck" at the first stream in the flatMap, which makes sense.</p>
  
  <p>With parallel I don't know if there is an expected behaviour, but my guess would be that it somehow interleaved the first <code>n</code> or so streams, where <code>n</code> is the number of parallel workers. It could also be slightly different based on the chunking/buffering behaviour.</p></li>
  <li><p>What causes it to run out of memory? <em>I'm specifically trying to understand how these streams are implemented under the hood.</em></p>
  
  <p>I'm guessing something blocks the stream, so it never finishes and is able to get rid of the generated values, but I don't quite know in which order things are evaluated and where buffering occurs.</p></li>
  </ol>
  
  <p><em>Edit:</em> In case it is relevant, I'm using Java 11.</p>
  
  <p><em>Editt 2:</em> Apparently the same thing happens even for the simple program <code>IntStream.iterate(1,i-&gt;i+1).limit(1000_000_000).parallel().sum()</code>, so it might have to do with the lazyness of <code>limit</code> rather than <code>flatMap</code>.</p>