Memory leak in ServiceProviderCache?
See original GitHub issueI have an application which has a timer, and every minute it does this:
using (var context = new MyDbContext())
{
// do some work
context.SaveChanges();
}
Gradually over time the memory usage of the process has been going up, and eventually it crashed with an OutOfMemoryException.
So I ran it again, took a memory dump, and investigated where the memory was going:
The ServiceProviderCache seems to gradually be growing with a new ServiceProvider every time it creates a context, and doesn’t seem to get freed on the Dispose.
Steps to reproduce
for (int i = 0; i < 1000; i++)
{
using (var context = new MyDbContext())
{
context.SomeSet.Add(new SomeEntity());
context.SaveChanges();
}
}
then check memory usage
Further technical details
EF Core version: 2.0.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: VS 2017
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:15 (6 by maintainers)
Top Results From Across the Web
ServiceProvider not releasing memory for transient EF ...
Data context registered as transient, but memory usage keeps growing. Is there something wrong with my DI configuration? Hot Network Questions.
Read more >Investigating a Memory Leak in Entity Framework Core
So, we spent some time investigating and incrementally making changes to resolve what looked like a classic memory leak. A common cause of...
Read more >Debug a memory leak in .NET Core
A memory leak may happen when your app references objects that it no longer needs to perform the desired task.
Read more >Memory Leaks using Dependency Injection with .NET Core
I inherited a .NET Core application that had a memory leak. I found the source to be the way the application used the...
Read more >8 Ways You can Cause Memory Leaks in .NET
8 Ways You can Cause Memory Leaks in .NET · 1. Subscribing to Events · 2. Capturing members in anonymous methods · 3....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gallivantor It is the creation of a new ILoggerFactory instance for each context instance that is causing the problem. Instead you should have one shared ILoggerFactory, or at least a very small number, shared by all your context instances. I agree that this is pretty unrecoverable–we will discuss what we can do to mitigate the issue.
I am having the same issue as @voroninp on 3.1.13. It seems if I add an entity to my DbContext, that context will never be cleaned up, which sounds a lot like the issue described in #21092. I have a generic add method I am using in my context:
TEntity ICamaContext.Add<TEntity>(TEntity entity) { return this.Set<TEntity>().Add(entity).Entity; }
This is the retention graph from Redgate ANTS Memory Profiler. CamaContext has been disposed; ServiceProviderEngineScope has not.