You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
voidInitDatabase(WebApplicationwebApplication){usingvarscope=webApplication.Services.CreateScope();usingvarcontext=scope.ServiceProvider.GetRequiredService<EmployeeContext>();context.Database.EnsureDeleted();context.Database.EnsureCreated();}voidRunSeeder(WebApplicationwebApplication,stringconnectionString){usingvarscope=webApplication.Services.CreateScope();usingvarcontext=scope.ServiceProvider.GetRequiredService<EmployeeContext>();// tested with manually create EmployeeContext - the same consumption//var optionsBuilder = new DbContextOptionsBuilder<EmployeeContext>();//optionsBuilder.UseSqlServer(connectionString);//using var context = new EmployeeContext(optionsBuilder.Options);varseeder=newEmployeeSeeder();seeder.Seed(context);}
without batches the consumption is greater
publicvoidSeed(EmployeeContextcontext){// if (!context.Employees.Any()){varrandom=newRandom();vardepartments=new[]{"IT","HR","Finance","Marketing","Sales"};varemployees=Enumerable.Range(1,100_000).Select(i =>newEmployee{FirstName=$"FirstName{i}",LastName=$"LastName{i}",Department=departments[random.Next(departments.Length)],IsActive=random.Next(0,2)==1,HireDate=DateTime.Now.AddDays(-random.Next(0,3650))}).ToList();varbatchSize=1_000;for(inti=0;i<employees.Count;i+=batchSize){varbatch=employees.Skip(i).Take(batchSize).ToList();context.Employees.AddRange(batch);context.SaveChanges();context.ChangeTracker.Clear();// not solve the problem}}}
EF Core version: 8.0.11
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11, Windows Server 2019
The text was updated successfully, but these errors were encountered:
Your sample includes lots of components, such as GraphQL, ASP.NET, etc. To relliably benchmark and isolate EF specifically, please extract the EF code into a minimal BenchmarkDotNet benchmark, using the [MemoryDiagnoer] to get memory results. At that point, if you still see unreasonable memory usage, please post the benchmark along with the results and we'll investigate.
BTW I'm noticing that you're instantiating 100k objects for the seeding, that in itself will already use up quite a lot of memory, which the GC won't necesary recollect right away (if it doesn't need to).
I'd advise using a memory profiler to see what the actual objects are which take up memory. You can also extract out the seeding to an external program, to isolate that as well.
Memory consumption, memory leaks after db operations
Code
Sample: https://github.com/Meteoeoeo/memory_consumption
after the first RunSeeder call the consumption increases significantly, in the next ones it increases but less
without batches the consumption is greater
some queries from logs
Include provider and version information
EF Core version: 8.0.11
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11, Windows Server 2019
The text was updated successfully, but these errors were encountered: