question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IdentityMap still referenced in StateManager after DbContexts has been returned to DbContextPool

See original GitHub issue

Using 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: image

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:closed
  • Created a year ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jonnybeecommented, Sep 27, 2022

@roji Will do but I’m on travel and conference this week so will send you code next week,

1reaction
rojicommented, Sep 24, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found