efcore: QueryCache: Memory leak when using dbContext.Set<> in subquery

When using dbContext.Set<MyTable1>() query cache is caching each instance that will fill memory with the same query.

The following is the result from https://github.com/Tasteful/bugs/blob/query-cache-memory-leak/MemoryUsage/Program.cs#L29-L56 where the only different is that the first result is using DbSet<> and the second using Set<>.

Query cache is working, using the dbContext.MyTable1
Before iteration 0 query cache count 0
After iteration 0 query cache count 1
Before iteration 1 query cache count 1
After iteration 1 query cache count 1
Before iteration 2 query cache count 1
After iteration 2 query cache count 1
Before iteration 3 query cache count 1
After iteration 3 query cache count 1
Before iteration 4 query cache count 1
After iteration 4 query cache count 1

Query cache is not working, using the dbContext.Set<MyTable1>()
Before iteration 0 query cache count 1
After iteration 0 query cache count 2
Before iteration 1 query cache count 2
After iteration 1 query cache count 3
Before iteration 2 query cache count 3
After iteration 2 query cache count 4
Before iteration 3 query cache count 4
After iteration 3 query cache count 5
Before iteration 4 query cache count 5
After iteration 4 query cache count 6

This issue is probably related to #6737 that have fixes for DbSet<>.

Steps to reproduce

Full running example exists here https://github.com/Tasteful/bugs/tree/query-cache-memory-leak

Further technical details

EF Core version: 1.1.1 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: VS2017

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 28 (28 by maintainers)

Most upvoted comments

@Tasteful Great to hear. The difficulty extending is an oversight, I will file an issue to fix.

@Tasteful Seems my theory for the bug was off a bit. Glad you found a workaround; we will still discuss if this should go in a patch.