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
use a shared cancellationTokenSource, memory usage is getting too high to be freed:
use CancellationToken.None:
Example
usingMicrosoft.EntityFrameworkCore;usingMicrosoft.Extensions.DependencyInjection;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;internalclassProgram{privatestaticIServiceProvider_serviceProvider=CreateServiceProvider();publicstaticasyncTaskMain(string[]args){//If you use a Shared CancellationTokenSource, will cause the memory cannot be releasedvarshareTokenSource=newCancellationTokenSource();awaitInitDbAsync(shareTokenSource.Token);awaitWriteToDbAsync(shareTokenSource.Token);//If you use CancellationToken.None, no such problem occursawaitInitDbAsync(CancellationToken.None);awaitWriteToDbAsync(CancellationToken.None);}publicstaticasyncTaskInitDbAsync(CancellationTokencancellationToken){awaitusingvarcontext=await_serviceProvider.GetRequiredService<IDbContextFactory<MyDbContext>>().CreateDbContextAsync(cancellationToken);awaitcontext.Database.EnsureDeletedAsync(cancellationToken);awaitcontext.Database.EnsureCreatedAsync(cancellationToken);}publicstaticasyncTaskWriteToDbAsync(CancellationTokencancellationToken){for(vari=0;i<1000;i++){awaitusingvarcontext=await_serviceProvider.GetRequiredService<IDbContextFactory<MyDbContext>>().CreateDbContextAsync(cancellationToken);vardata=Enumerable.Range(0,1000).Select(_ =>newTableA()).ToList();awaitcontext.AddRangeAsync(data,cancellationToken);awaitcontext.SaveChangesAsync(cancellationToken);Console.WriteLine($"{i} TotalMemory: {GC.GetTotalMemory(true)}");}}publicstaticIServiceProviderCreateServiceProvider(){varservices=newServiceCollection();services.AddDbContextFactory<MyDbContext>(builder =>{builder.UseSqlServer("Data Source=.;Initial Catalog=DbMemoryTest;MultipleActiveResultSets=True;Encrypt=False;Integrated Security=SSPI", b =>{b.EnableRetryOnFailure();});});returnservices.BuildServiceProvider();}publicclassMyDbContext:DbContext{publicDbSet<TableA>TableAs{get;set;}publicMyDbContext(DbContextOptionsoptions):base(options){}}publicclassTableA{[Key][DatabaseGenerated(DatabaseGeneratedOption.Identity)]publicintId{get;set;}publicGuidProperty1{get;set;}=Guid.NewGuid();publicGuidProperty2{get;set;}=Guid.NewGuid();publicGuidProperty3{get;set;}=Guid.NewGuid();publicGuidProperty4{get;set;}=Guid.NewGuid();}}
Possible reasons
Data is always in ExecutionContext
Include provider and version information
EF Core version: 7.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 11 22H2
IDE: Visual Studio 2022 17.5.2
The text was updated successfully, but these errors were encountered:
Ask a question
use a shared cancellationTokenSource, memory usage is getting too high to be freed:
use CancellationToken.None:
Example
Possible reasons
Data is always in ExecutionContext
Include provider and version information
EF Core version: 7.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 11 22H2
IDE: Visual Studio 2022 17.5.2
The text was updated successfully, but these errors were encountered: