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

HierarchicalLifetimeManager prevents GC from collecting scopes #297

Open
ENikS opened this issue Dec 12, 2020 · 0 comments
Open

HierarchicalLifetimeManager prevents GC from collecting scopes #297

ENikS opened this issue Dec 12, 2020 · 0 comments
Assignees
Labels
Enhancement 🔨 Improvement of existing features
Milestone

Comments

@ENikS
Copy link
Contributor

ENikS commented Dec 12, 2020

HierarchicalLifetimeManager

A special lifetime manager creates singletons within hierarchies. It would return the same object as long as resolution is happening at the same hierarch level. If resolved in a child container, a new object would be created.

Problem

When object is resolved in one of the child containers, the manager creates and maintains a strong reference to that container. It is used later to identify a value associated with that scope.

If the child container is properly discarded, when it is no longer needed, by calling IDisposable.Dispose(), the manager releases the reference and scope is collected by GC.

If the child container is released without being disposed, the manager has no way of knowing that it is no longer alive and would
still keep the reference preventing the scope from being collected.

This behavior could potentially contribute to a memory leaks and performance degradation.

Solution

Instead of using ConcurrentDictionary for storing references to the scopes a custom solution utilizing Weak References should be implemented.

@ENikS ENikS added the Enhancement 🔨 Improvement of existing features label Dec 12, 2020
@ENikS ENikS added this to the 6.0.0 milestone Dec 12, 2020
@ENikS ENikS self-assigned this Dec 12, 2020
@github-actions github-actions bot added the release Release branch label Dec 15, 2020
@ENikS ENikS removed the release Release branch label Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement 🔨 Improvement of existing features
Projects
None yet
Development

No branches or pull requests

1 participant