title: Parallel.ForEach can cause a "Out Of Memory" exception if working with a enumerable with a large object
body: <p>I am trying to migrate a database where images were stored in the database to a record in the database pointing at a file on the hard drive. I was trying to use <code>Parallel.ForEach</code> to speed up the process <a href="https://stackoverflow.com/questions/3096727/is-there-a-way-to-use-the-task-parallel-librarytpl-with-sqldatareader/3097049#3097049">using this method</a> to query out the data.</p>
  
  <p>However, I noticed that I was getting an <code>OutOfMemory</code> Exception. I know <code>Parallel.ForEach</code> will query a batch of enumerables to mitigate the cost of overhead if there is one for spacing the queries out (so your source will more likely have the next record cached in memory if you do a bunch of queries at once instead of spacing them out). The issue is due to one of the records that I am returning is a 1-4Mb byte array that caching is causing the entire address space to be used up (The program must run in x86 mode as the target platform will be a 32-bit machine)</p>
  
  <p>Is there any way to disable the caching or make is smaller for the TPL?</p>
  
  <hr>
  
  <p>Here is an example program to show the issue. This must be compiled in the x86 mode to show the issue if it is taking to long or is not happening on your machine bump up the size of the array (I found <code>1 &lt;&lt; 20</code> takes about 30 secs on my machine and <code>4 &lt;&lt; 20</code> was almost instantaneous)</p>
  
  <pre><code>class Program
  {
  
      static void Main(string[] args)
      {
          Parallel.ForEach(CreateData(), (data) =&gt;
              {
                  data[0] = 1;
              });
      }
  
      static IEnumerable&lt;byte[]&gt; CreateData()
      {
          while (true)
          {
              yield return new byte[1 &lt;&lt; 20]; //1Mb array
          }
      }
  }
  </code></pre>