title: Large Gen 0 Heap in .NET, wasting memory?
body: <p>I have an ASP.NET / C# web application that is using a lot of memory.</p>
  
  <p>ANTS Memory Profiler and PerfMon both show my Gen 0 heap growing rapidly to about 1 GB in size during Application_Start.  I read <a href="http://msdn.microsoft.com/en-us/magazine/cc163528.aspx" rel="noreferrer">here</a> that the PerfMon counter for Gen 0 Heap is actually showing the "budget" for Gen 0, not the size (which I take to mean not all that memory is part of the private working set of the process?).  However the ANTS profiler does show about 700 MB of "unused memory allocated to .NET", and this <em>does</em> seem to be part of the private working set of the process (as reported in taskmgr).  I am guessing this large amount of unused memory is related to the large Gen 0 heap.</p>
  
  <p>What is happening in Application_Start while this happens is that I'm in a while loop reading about a million rows from a SqlDataReader.  These are being used to populate a large cache for later use.  Given this, the obvious culprit for the large amount of unused memory was large object heap fragmentation, but I don't think this is the case as I'm pre-allocating more than is needed for my large cache object.  To be certain, I even tried commenting out the part of the loop that actually adds to my cache object; it made no difference in the amount of unused memory allocated.</p>
  
  <p>As a test, I tried frequently forcing garbage collection of gen 0 during the loop (against all recommendations, I know), and this caused the size of the gen0 heap to stay down around 128 MB and also resulted in only a few MB of unused free memory.  But it also maxed out my CPU and made Application_Start take way too long.</p>
  
  <p>My questions are:</p>
  
  <p>1) What can cause the reported size of the Gen 0 Heap to grow so large?</p>
  
  <p>2) Is this a problem?  In particular, could it be causing a large amount of unused space to be allocated to .NET?</p>
  
  <p>3) If so, what should I do to fix it?  If I can't prevent the process from using that much memory during Application_Start, I'd like to at least be able to make it give up the memory when app start completes.</p>