title: Large object heap waste
body: <p>I noticed that my application runs out of memory quicker than it should. It creates many byte arrays of several megabytes each. However when I looked at memory usage with vmmap, it appears .NET allocates much more than needed for each buffer. To be precise, when allocating a buffer of 9 megabytes, .NET creates a heap of 16 megabytes. The remaining 7 megabytes cannot be used to create another buffer of 9 megabytes, so .NET creates another 16 megabytes. So each 9MB buffer wastes 7MB of address space!</p>
  
  <p>Here's a sample program that throws an OutOfMemoryException after allocating 106 buffers in 32-bit .NET 4:</p>
  
  <pre><code>using System.Collections.Generic;
  
  namespace CSharpMemoryAllocationTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              var buffers = new List&lt;byte[]&gt;();
              for (int i = 0; i &lt; 130; ++i)
              {
                  buffers.Add(new byte[9 * 1000 * 1024]);
              }
  
          }
      }
  }
  </code></pre>
  
  <p>Note that you can increase the size of the array to 16 * 1000 * 1024 and still allocate the same amount of buffers before running out of memory.</p>
  
  <p>VMMap shows this:</p>
  
  <p><img src="https://i.stack.imgur.com/ETJRE.png" alt="enter image description here"></p>
  
  <p>Also note how there's an almost 100% difference between the total Size of the Managed Heap and the total Commited size. (1737MB vs 946MB).</p>
  
  <p>Is there a reliable way around this problem on .NET, i.e. can I coerce the runtime into allocating no more than what I actually need, or maybe much larger Managed Heaps that can be used for several contiguous buffers?</p>