Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using IQueryable.Contains() in an expression causes a memory leak #11015

Closed
jessebarocio opened this issue Feb 20, 2018 · 2 comments
Closed

Using IQueryable.Contains() in an expression causes a memory leak #11015

jessebarocio opened this issue Feb 20, 2018 · 2 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@jessebarocio
Copy link

jessebarocio commented Feb 20, 2018

If you use IQueryable<T>.Contains() in an expression it will cause a memory leak. The leak appears to be in the CompiledQueryCache as memory snapshots show a MemoryCache growing like crazy.

snapshot

The obvious workaround is to not use an IQueryable in the expression. I never intended to and I've already fixed it. But it was difficult to track down the leak so I still think it should be fixed if possible.

Steps to reproduce

Here's a complete sample to reproduce the issue.

class Program
{
    private static void Main(string[] args)
    {
        using (var context = new Context())
        {
            context.Database.EnsureCreated();
            for (int i = 0; i < 10000; i++)
            {
                var genders = (new[] { Gender.MALE, Gender.FEMALE }).AsQueryable();
                context.Persons.Where(p => genders.Contains(p.Gender)).ToList();  
            }
        }
        
        Console.WriteLine("Done.");
        Console.ReadKey();
    }
}

class Context : DbContext
{
    public DbSet<Person> Persons { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlite("Data Source=memleak.db");
}

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Gender Gender { get; set; }
}

public enum Gender { UNKNOWN, MALE, FEMALE };

Further technical details

EF Core version: 2.0.1
Database Provider: Tested with SqlServer & Sqlite providers
Operating system: Windows 10
IDE: Visual Studio 2017 15.5.2

@smitpatel
Copy link
Contributor

Related #9301

@smitpatel
Copy link
Contributor

@jessebarocio - We get separate cache entry for each query since, EnumerableQueries are opaque to us. We would not know genders would enumerate too. Hence they don't match existing cache entry. If you move genders declaration outside then we do reference equality on value causing cached entry to be used.

If we parametrize the Queryable then we can use same query cache while still enumerating to different SQL. Current pipeline already supports that with Contains. We will be doing that as a part of fixing this issue.

smitpatel added a commit that referenced this issue Feb 22, 2018
Issue: Non-EF IQueryables are opaque to us so we cannot compare them causing new cache entry for each of them. But we have mechanism for running a query with parameter of type enumerable using 2nd level cache.
In this case, we can just parametrize non-EF IQueryable to cause a parameter which gets inlined for Contains in Sql

Resolves #11015
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 22, 2018
smitpatel added a commit that referenced this issue Feb 23, 2018
Issue: Non-EF IQueryables are opaque to us so we cannot compare them causing new cache entry for each of them. But we have mechanism for running a query with parameter of type enumerable using 2nd level cache.
In this case, we can just parametrize non-EF IQueryable to cause a parameter which gets inlined for Contains in Sql

Resolves #11015
smitpatel added a commit that referenced this issue Feb 23, 2018
Issue: Non-EF IQueryables are opaque to us so we cannot compare them causing new cache entry for each of them. But we have mechanism for running a query with parameter of type enumerable using 2nd level cache.
In this case, we can just parametrize non-EF IQueryable to cause a parameter which gets inlined for Contains in Sql

Resolves #11015
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants