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

Use static CoreClrAssemblyLoader in the SdkResolverLoader #6864

Merged

Conversation

marcin-krystianc
Copy link
Contributor

Fixes #6842 (comment)

Context

We use static for the CoreClrAssemblyLoader field so each unique SDK resolver assembly is loaded into memory and JITted only once. All subsequent load requests will return assembly from the assembly loader's cache instead of loading it again from disk. This change increases the performance of SDK resolution and at the same time avoids leaking memory due to loading the same SDK resolver assembly multiple times and never unloading it.

Changes Made

The CoreClrAssemblyLoader field in the SdkResolverLoader class was changed from non-static to static member. The same instance of CoreClrAssemblyLoader will be used by all instances of SdkResolverLoader. It is consistent now with other uses of CoreClrAssemblyLoader in msbuild.

Testing

Tested manually using repro from #5037 (comment)

Notes

Alternative approach would be to use collectible CoreClrAssemblyLoader / AssemblyLoadContext - that would fix the leak as well but it would be less performant as it wouldn't benefit from re-using already loaded and JITed assemblies.

We want to use static for CoreClrAssemblyLoader so each unique SDK resolver assembly is loaded into memory and JITted only once.
Copy link
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've validated that the initial bug hits even in dotnet build scenarios -- we were accidentally loading SDK resolvers approximately one bajillion times more than we needed to.

This looks like a great fix. Thanks @marcin-krystianc!

Copy link
Member

@Forgind Forgind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Forgind Forgind added the merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now. label Sep 17, 2021
@stackedsax
Copy link

"one bajillion times" 😆

Thanks, @rainersigwald and @Forgind for jumping on it.

@AR-May AR-May merged commit 57f14a7 into dotnet:main Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SDK Resolver assemblies leak AssemblyLoadContexts
5 participants