title: High memory usage by ASP.NET applications
body: <p>We have an issue with some of our ASP.Net applications. Some of our apps claim a large amount of memory from start as their working set.</p>
  
  <p>On our 2 webfarm-servers (4gb of RAM each) run multiple applications. We have a stable environment with about 1.2gb of memory free.</p>
  
  <p>Then we add an MVC5 + WebApi v2 + Entity Framework app that instantly claims 1+gb as working set memory, while only actually using about 300mb. This causes other applications to complain that there is not enough memory left.</p>
  
  <p>We already tried setting limit for virtual memory and the limit for private memory, without any avail. If we set this to about 500mb, the app still uses more or less the same amount of memory (way over 500) and does not seem to respect the limits put in place.</p>
  
  <p>For reference, I tested this with an empty MVC5 project (VS2013 template) and this already claims 300mb of memory, while only using about 10mb.</p>
  
  <p>Setting the app as a 32-bit app seems to have some impact in reducing the size of the working set.</p>
  
  <p>Is there any way to reduce the size of the working set, or to enforce a hard limit on the size of it? </p>
  
  <p>Edit:
  In the case of the huge memory use for the project using Web Api v2 and Entity Framework, my API controllers look like this:</p>
  
  <pre><code>namespace Foo.Api
  {
  public class BarController : ApiController
  {
      private FooContext db = new FooContext(); 
  
      public IQueryable&lt;Bar&gt; GetBar(string bla)
      {
          return db.Bar.Where(f =&gt; f.Category.Equals(bla)).OrderBy(f =&gt; f.Year);
      }
  }
  </code></pre>
  
  <p>as they look in most tutorials I could find (including the ones from microsoft). Using <code>using</code> here does not work because of LINQ deferred loading. It could work if I added a ToList (not tested) everywhere, but does this have any other impact?</p>
  
  <p>edit2:
  It works if I do</p>
  
  <pre><code>namespace Foo.Api
  {
  public class BarController : ApiController
  {
      public List&lt;Bar&gt; GetBar(string bla)
      {
          using(FooContext db = new FooContext){
             return db.Bar.Where(f =&gt; f.Category.Equals(bla)).OrderBy(f =&gt; f.Year).ToList();
          }
      }
  }
  </code></pre>
  
  <p>Does the ToList() have any implications on the performance of the api? (I know I can not continue querying cheaply as with an IQueryable)</p>
  
  <p>Edit3:
  I notice that its the private working set of the app that is quite high. Is there a way to limit this? (Without causing constant recycles)</p>
  
  <p>Edit4:
  As far as I know I have a Dispose on each and every APIController. My front-end is just some simple MVC controllers but for the large part .cshtml and javascript (angular) files.</p>
  
  <p>We have another app, just regular mvc, with two models and some simple views (no database, or other external resources that could be leaked) and this also consumes up to 4-500mb of memory. If I profile it, I can't see anything that indicates memory leaks, I do see that only 10 or 20 mb is actually used, the rest is unmanaged memory that is unassigned (but part of the private memory working set, so claimed by this app and unusable by any other).</p>