efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Memory leak in ServiceProviderCache?

Open gallivantor opened this issue 9 years ago • 21 comments

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

gallivantor avatar Nov 03 '17 12:11 gallivantor

@gallivantor There are a few pathological things that can be done when configuring the context that will cause this to happen. Can you share the code used to configure your context? (i.e the code in OnConfiguring or AddDbContext.)

Also, you should see a warning in the logs for this--did you see that?

ajcvickers avatar Nov 03 '17 15:11 ajcvickers

I do see this warning logged: More than twenty 'IServiceProvider' instances have been created for internal use by Entity Framework. Consider reviewing calls on 'DbContextOptionsBuilder' that may require new service providers to be built.

The DbContext is as follows (I've changed some names as per company policy). I don't think I'm doing anything particularly edge-case here; the documentation isn't very forthcoming about what the possible causes of this warning might be?

    public class MyDbContext : DbContext
    {
        public DbSet<MyType1> Table1 { get; set; }
        public DbSet<MyType2> Table2 { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(/* CONN STRING */, opt =>
            {
                opt.EnableRetryOnFailure();
                opt.CommandTimeout(60);
            });
            optionsBuilder.UseLoggerFactory(new EfConsoleLoggerFactory());

            optionsBuilder.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
            
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ConfigureTable<MyType1>(modelBuilder, "Table1");
            ConfigureTable<MyType2>(modelBuilder, "Table2");
        }

        private void ConfigureTable<T>(ModelBuilder modelBuilder, string tableName) where T : MyBaseTable
        {
            modelBuilder.Entity<T>().ToTable(tableName).HasKey(e => e.Id);
        }
    }

gallivantor avatar Nov 06 '17 09:11 gallivantor

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

ajcvickers avatar Nov 06 '17 18:11 ajcvickers

Great thanks, yep that seems to have fixed it.

I think as this issue isn't very discoverable -- and is the sort of problem that someone could easily put into Production causing a slow-burning memory leak -- it would be good if a way could be found to change this behaviour; or at least update the docs to make clear what causes this problem, and perhaps re-word that warning message to be explicit about the fact that it could be creating a memory leak. For example I think I copied that UseLoggerFactory line from a StackOverflow answer somewhere and if other people are doing the same they could be unwittingly introducing a similar bug.

On a side-note, is it possible to use the DBContext Pooling with other DI frameworks? Using the pooling would also work around this problem, but as far as I can tell it's tied into the Microsoft.Extensions.DependencyInjection DI framework (services.AddDbContextPool) and I have an existing app which is built around using Unity directly

By the way, good work with EF Core. It's great to be able to interact with the team here on Github, it gives us much more confidence to use the product knowing that we can get answers easily if problems come up.

gallivantor avatar Nov 07 '17 09:11 gallivantor

@gallivantor Yep, I started work on updating the docs yesterday. Hope to send them out for review today.

ajcvickers avatar Nov 07 '17 16:11 ajcvickers

Filed "Add fwlnk" and "Make warning error by default" and also submitted docs PR to update the link.

ajcvickers avatar Nov 07 '17 23:11 ajcvickers

Awesome! I was having the same issue as well. I was creating the LoggerFactory on every DbContext instance request.

codematrix avatar Jun 03 '18 18:06 codematrix

Hm... I have similar problem with 3.1.5

image

DbContext is disposed but not released.

voroninp avatar Sep 26 '20 16:09 voroninp

@ajcvickers , here is the repo to reproduce: https://github.com/voroninp/EFCoreMemoryLeak

dotMemory shows me disposed yet not collected instances of AppDbContext, and Entity instances, which stay alive even after several forced GC runs.

image

voroninp avatar Sep 26 '20 22:09 voroninp

Also in tests with NServiceBus I have

ObjectDisposedException: Cannot access a disposed object. Object name: 'IServiceProvider'.

thrown by the NOT YET disposed context (_disposed: fale) when I try to access ChangeTracker.

at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException() in //src/DependencyInjection/DI/src/ServiceLookup/ThrowHelper.cs:line 14 at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) in //src/DependencyInjection/DI/src/ServiceLookup/ServiceProviderEngineScope.cs:line 34 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Internal.ScopedLoggerFactory.Create(IServiceProvider internalServiceProvider, IDbContextOptions contextOptions) at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.b__7_0(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) in //src/DependencyInjection/DI/src/ServiceLookup/ServiceProviderEngine.cs:line 88 at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) in //src/DependencyInjection/DI/src/ServiceLookup/ServiceProviderEngineScope.cs:line 37 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker() at CarNext.Hydra.Data.Repositories.HydraDataUnitOfWork.Dispose(

I suspect this code in DI container may be the reason for the bug:

private List<object> BeginDispose()
{
    List<object> toDispose;
    lock (ResolvedServices)
    {
        if (_disposed)
        {
            return null;
        }

        _disposed = true;
        toDispose = _disposables;
        _disposables = null;

        // Not clearing ResolvedServices here because there might be a compilation running in background
        // trying to get a cached singleton service instance and if it won't find 
        // it it will try to create a new one tripping the Debug.Assert in CaptureDisposable
        // and leaking a Disposable object in Release mode
    }

    return toDispose;
}

First it sets itself as disposed and then returns services to dispose.

voroninp avatar Sep 28 '20 12:09 voroninp

@voroninp Disposing and garbage collection are not tightly coupled. That being said, if you believe there is a problem with D.I., then please file an issue at https://github.com/dotnet/runtime.

ajcvickers avatar Sep 29 '20 19:09 ajcvickers

@ajcvickers Look at the project I referenced, the leak is reproducible.

As to Disposing that's probably another issue somehow caused by how NSB works with DI. For me it's a mystery how internal service provider gets disposed.

voroninp avatar Sep 29 '20 20:09 voroninp

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

BabellDev avatar Apr 16 '21 13:04 BabellDev

Is there any new information on this? Seems like I ran into the same problem.

denisulmer avatar Nov 01 '22 23:11 denisulmer

@denisulmer If you are seeing an EF problem here, then please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Nov 02 '22 11:11 ajcvickers

Hi I have this memory issue which makes my SelfHosted service keep increasing without release image What could be the issue, now it has used 400mb and it just grows until manual restart. thanks for the help

yoganr avatar Nov 10 '23 14:11 yoganr

When digg more I just see this Db context and SqlConnection a lot image

yoganr avatar Nov 10 '23 14:11 yoganr

Hi @yoganr, I'm curious what version of efcore you are running? We recently upgraded our project to efcore 6 and I haven't had a chance to profile it again. I will try to report back here when I do.

BabellDev avatar Nov 10 '23 14:11 BabellDev

We had this issue with ef core 3.1 and then we hoped upgrading to 7.0 will fix it but seems like same problems, what I sent you was using ef core 7.0

yoganr avatar Nov 10 '23 14:11 yoganr

@BabellDev the version was ef core 6.0 not 7.0

yoganr avatar Nov 10 '23 15:11 yoganr