-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
When I start my application, gradually, as processes are executed, the memory consumption just keeps increasing until my k8s pod simply dies due to lack of memory and gets restarted.
After analyzing the generated dumps, we observed the following:
To ensure that the dispose method of my DbContext is always executed, some logs were added during its creation and disposal.
public MyDbContext(IDatabaseConfig configuration,
ISeed seed,
ILogger<MyDbContext> logger)
{
_contextId = Guid.NewGuid();
_logger = logger;
_logger.LogInformation("Context created. ID {0}", _contextId.ToString());
_configuration = configuration;
_seed = seed;
}
public override void Dispose()
{
base.Dispose();
_logger.LogInformation("Context destroyed. ID {0}", _contextId.ToString());
}
public override ValueTask DisposeAsync()
{
var result = base.DisposeAsync();
_logger.LogInformation("Context destroyed. ID {0}", _contextId.ToString());
return result;
}
And from these logs, we were able to confirm that the object is indeed always created and disposed without any problems.
In some previous issues, I observed some possible causes within the OnConfiguring method, but I believe that is not the problem in my case.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseNpgsql(_configuration.ConnectionString);
base.OnConfiguring(optionsBuilder);
}
My DbContext instance is resolved in this way:
private static void RegisterContext(IServiceCollection services)
=> services.AddDbContext<IMyDbContext, MyDbContext>();
The issue was created here because Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache is indicated. But perhaps the problem might be related to the GarbageCollector?
EF Core version: 8.0.6
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.10.1
My container is running with this base image: mcr.microsoft.com/dotnet/aspnet:8.0