Use static CoreClrAssemblyLoader in the SdkResolverLoader #6864
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theSdkResolverLoader
class was changed from non-static to static member. The same instance ofCoreClrAssemblyLoader
will be used by all instances ofSdkResolverLoader
. It is consistent now with other uses ofCoreClrAssemblyLoader
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.