IdentityMap still referenced in StateManager after DbContexts has been returned to DbContextPool
See original GitHub issueUsing EF Core 7.0.0-rc.2.22464.4 Database provider: Microsoft.EntityFrameworkCore.SqlServer IDE: VisualStudio 2022 v17.3.4 OS: Windows 11
Memory is still referenced for IdentityMap in StateManager after DbContexts have been returned to DbContextPool using DbContextFactory:
After testing the same issue in release/6.0 this will change in StateManager.Clear method will fix the issue:
public virtual void Clear()
{
Unsubscribe();
ChangedCount = 0;
_entityReferenceMap.Clear();
_referencedUntrackedEntities = null;
_identityMaps?.Clear();
_identityMaps = null!;
_identityMap0 = null!;
_identityMap1 = null!;
.....
Issue Analytics
- State:
- Created a year ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
Proxies retain a reference to pooled DbContext after it is ...
The issue appears to be with disposing a database instance when using AddDbContextPool (which I was doing unwittingly). Its disposal doesn't cause an...
Read more >DbContext & DbcontextPool in ef-core
"DbContext is not thread-safe." This statement means that it's not safe to access a DbContext from multiple threads in parallel.
Read more >Accessing Tracked Entities - EF Core
Using DbSet.Local to query tracked entities ... EF Core queries are always executed on the database, and only return entities that have been...
Read more >Managing DbContext the right way with Entity Framework 6
A bit of context. This isn't the first post that has been written about managing the DbContext lifetime in Entity Framework-based applications.
Read more >Best Practices in Using the DbContext in EF Core
This article is about DbContext concepts and the best practices you can adhere to for improving the performance of your applications built ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@roji Will do but I’m on travel and conference this week so will send you code next week,
@jonnybee I’ve looked at this again, and I still can’t see how this makes sense… As @ajcvickers wrote above, we do clear the dictionaries referenced by the StateManager. Dictionary.Clear itself does Array.Clear on Dictionary’s two internal arrays, which should release any objects referenced by them - but does not decrease the size of the arrays themselves; however, I can’t imagine that being the source of the size.
To make sure we’re not missing something, can you submit the code sample you’re using to repro this? I have a code snippet from you in the email, but it isn’t full (e.g. the model is missing); if you can submit a minimal console program, I’ll investigate using that.