question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak in ServiceProviderCache?

See original GitHub issue

I 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:

image

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:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
ajcvickerscommented, Nov 6, 2017

@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.

5reactions
BabellDevcommented, Apr 16, 2021

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.

add-retention

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found