title: C# EntityFramework IQueryable Memory Leak
body: <p>We're seeing memory resources not be released:</p>
  
  <p><a href="https://i.stack.imgur.com/fhmdu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fhmdu.png" alt="enter image description here"></a></p>
  
  <p>With the following code using .NET Core:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          while (true) {          
              var testRunner = new TestRunner();
              testRunner.RunTest();
          }
      }
  }
  
  public class TestRunner {
      public void RunTest() {
          using (var context = new EasyMwsContext()) {
              var result = context.FeedSubmissionEntries.Where(fse =&gt; TestPredicate(fse)).ToList();
          }
      }
  
      public bool TestPredicate(FeedSubmissionEntry e) {
          return e.AmazonRegion == AmazonRegion.Europe &amp;&amp; e.MerchantId == "1234";
      }
  }
  </code></pre>
  
  <p>If I remove the test predicate <code>.Where</code> I get a straight line as expected, with the predicate the memory will continue to rise indefinitely.</p>
  
  <p>So while I can fix the problem I'd like to understand what is happening?</p>
  
  <p>EDIT:</p>
  
  <p>Altering the line to:</p>
  
  <pre><code>public void RunTest() {
      using (var context = new EasyMwsContext()) {
          var result = context.FeedSubmissionEntries.ToList();
      }
  }
  </code></pre>
  
  <p>Gives the graph:
  <a href="https://i.stack.imgur.com/AW9q7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AW9q7.png" alt="enter image description here"></a></p>
  
  <p>So I don't believe this is due to client side evaluation either?</p>
  
  <p>EDIT 2:</p>
  
  <p>Using EF Core 2.1.4</p>
  
  <p>And the object heap:
  <a href="https://i.stack.imgur.com/bcOw0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bcOw0.png" alt="enter image description here"></a></p>
  
  <p>Edit 3:</p>
  
  <p>Added a retention graph, seems to be an issue with EF Core?</p>
  
  <p><a href="https://i.stack.imgur.com/cGIKr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cGIKr.png" alt="enter image description here"></a></p>