title: .NET 4 WCF memory issue
body: <p>I hit issue that my <strong>ASP.NET 4 MVC 2 + WCF</strong> application utilize lots of memory in <strong>Windows 2008 64-bit</strong> application during the load testing where it will use up almost all the available memory <strong>(8 GB)</strong> after few minutes run (we did have few worker process running). </p>
  
  <p>After profiling using <strong>ANTS Memory Profiler</strong> it was showing few interesting outcome:</p>
  
  <ol>
  <li>.NET managed memory  increase from 15 MB to 40 MB, However this is attributed to caching mechanism that we did in the program. <strong>However .NET itself allocate allocate almost 180 MB Free Space, which is unexpected.</strong></li>
  <li><strong>Unmanaged Memory size increases significantly until 120 MB after the load test just running about 3 minutes (although our application didn't explicitly use any P/Invoke or COM object. However we did use a few COM+ object which is disposed after used in the finally block).</strong> </li>
  <li><strong>Memory become fragmented.</strong></li>
  <li>Both item no 1 &amp; 2 above causing the whole application to <strong>use about 350 MB</strong> just after the load test running a few minutes but if we didn't stop the test it will continue to grow further.</li>
  </ol>
  
  <p>Based on item no 1 above, I tested some application to test whether the issue is due to our application or WCF. <em>The test application just load XML data (about 300KB) to dataset in a multithreading application</em>. When the logic is stored in EXE program, the application use only 200 KB (additional 120 KB from beginning with 40 KB for unused memory) managed memory from 24 MB private bytes after finish (which is acceptable); <strong>but when the logic is hosted in WCF, the application uses 66 MB managed memory (additional 61 MB from beginning with 64 MB free/unused managed memory). So it seems that WCF / ASP.NET is the one that causing the memory to increase a lot).</strong></p>
  
  <ol>
  <li><strong>Why .NET allocate so much free space in the heap?</strong> Understood that the free space could be some Gen 0/Gen 1/Gen 2 that is GC-ed during memory snapshot process, but I don't think the application really use up that much memory. </li>
  <li><strong>is the behavior normal for WCF? If yes, any way to change the behavior so that it uses lesser memory?</strong></li>
  <li><strong>How to find unmanaged memory leak especially that I didn't use the unmanaged code explicitly?</strong></li>
  </ol>
  
  <p>Appreciate your advise on the question above.</p>
  
  <p>Thanks in advance,</p>
  
  <p>Willy</p>