Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible memory leak when use PooledDbContextFactory #32652

Closed
FarseerSky opened this issue Dec 20, 2023 · 4 comments · Fixed by #32871
Closed

Possible memory leak when use PooledDbContextFactory #32652

FarseerSky opened this issue Dec 20, 2023 · 4 comments · Fixed by #32871
Labels
area-perf area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@FarseerSky
Copy link

Issue Description:
I have encountered a memory dump issue during my stress test. The memory dump shows that there are 95000 instances of EntityFrameworkCore.Update.ColumnModification held by 239 DbContext objects in the pool.
image

I have investigated the source code and found that in the TopologicalSort method of CommandBatchPreparer, it clears the _modificationCommandGraph field which stores all the commands from the last time only when a new BatchCommands invoked. Can this be cleared after the SaveChangesAsync() method instead?

Code snippet:

await using var ctx = await _contextFactory.CreateDbContextAsync();
foreach (var bean in beans)
{
    ctx.Update(bean);
}

var res = await ctx.SaveChangesAsync();
ctx.ChangeTracker.Clear();

System Information:

  • EF Core version:8.0.0
  • Database provider: Pomelo.EntityFrameworkCore.MySql 8.0.0-beta.2
  • Target framework: NET 8.0
  • Operating system:Linux
@ajcvickers
Copy link
Member

/cc @AndriySvyryd

@ajcvickers ajcvickers added this to the 9.0.0 milestone Jan 18, 2024
AndriySvyryd added a commit that referenced this issue Jan 20, 2024
Reset StateManager and DatabaseFacade more consistently
Fix functional tests so they actually return the context to pool.

Fixes #32652
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 20, 2024
@AndriySvyryd AndriySvyryd removed their assignment Jan 20, 2024
AndriySvyryd added a commit that referenced this issue Jan 20, 2024
Reset StateManager and DatabaseFacade more consistently
Fix functional tests so they actually return the context to pool.

Fixes #32652
AndriySvyryd added a commit that referenced this issue Jan 22, 2024
Reset StateManager and DatabaseFacade more consistently
Fix functional tests so they actually return the context to pool.

Fixes #32652
AndriySvyryd added a commit that referenced this issue Jan 22, 2024
…32871)

Reset StateManager and DatabaseFacade more consistently
Fix functional tests so they actually return the context to pool.

Fixes #32652
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview1 Jan 31, 2024
@MoMack20
Copy link

Will this make it into a EF Core 8 patch? If not, is the call to ctx.ChangeTracker.Clear(); enough to mitigate this issue?

@mu88
Copy link

mu88 commented Apr 3, 2024

@MoMack20 no, unfortunately, calling context.ChangeTracker.Clear(); is not enough to mitigate the issue as you can see in my other issue

@FarseerSky
Copy link
Author

@mu88 Looking at the milestone, it should be fixed in version 9.0. I have given up using PooledDbContext, and the non-pooled performance is also very good.

@roji roji modified the milestones: 9.0.0-preview1, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-perf area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants