Search code examples
c#memory-leaks.net-coreentity-framework-core

C# EntityFramework IQueryable Memory Leak


We're seeing memory resources not be released:

enter image description here

With the following code using .NET Core:

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 => TestPredicate(fse)).ToList();
        }
    }

    public bool TestPredicate(FeedSubmissionEntry e) {
        return e.AmazonRegion == AmazonRegion.Europe && e.MerchantId == "1234";
    }
}

If I remove the test predicate .Where I get a straight line as expected, with the predicate the memory will continue to rise indefinitely.

So while I can fix the problem I'd like to understand what is happening?

EDIT:

Altering the line to:

public void RunTest() {
    using (var context = new EasyMwsContext()) {
        var result = context.FeedSubmissionEntries.ToList();
    }
}

Gives the graph: enter image description here

So I don't believe this is due to client side evaluation either?

EDIT 2:

Using EF Core 2.1.4

And the object heap: enter image description here

Edit 3:

Added a retention graph, seems to be an issue with EF Core?

enter image description here


Solution

  • I ended up running into the same issue. Once I knew what the problem was I was able to find a bug report for it here in the EntityFrameworkCore repository.

    The short summary is that when you include an instance method in an IQueryable it gets cached, and the methods do not get released even after your context is disposed of.

    At this time it doesn't look like much progress has been made towards resolving the issue. I'll be keeping an eye on it, but for now I believe the best options for avoiding the memory leak are:

    1. Rewrite your methods so no instance methods are included in your IQueryable
    2. Convert the IQueryable to a list with ToList() before using LINQ methods that contain instance methods (not ideal if you're trying to limit the results of a database query)
    3. Make the method you're calling static to limit how much memory piles up