-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Memory leak: Property caches are not getting cleaned up when using AssemblyParts. #21744
Comments
I’m sure there are more things that don’t get cleared when assemblies get unloaded. This isn’t something we’ve designed for so I would expect this to be the tip of the iceberg. If you are willing to send PRs to solve the issues as you did them, that would be great. Otherwise, we’d need to look at this all up but, that’s super unlikely because it’s low priority |
It could really be that there are more things that doesn't get cleaned up. But when analyzing the memory usage this seemed to be the most prominent error. I will try to further investigate the issue and try to provide a PR if I find the time. |
cc @janvorli |
It appears that not only the property caches do net get cleared but also the cache of the ModelBinderFactory is growing with each loading and unloading. |
I am looking at this issue and what if instead of clear cache we do not cache the type at all when it was loaded from a different |
Disabling the cache entirely for types loaded from non-default AssemblyLoadContexts seems like it would hurt perf pretty badly for affected scenarios. Could we just clear caches when the IActionDescriptorChangeProvider fires? |
@halter73 the perf regression is something I was concerned but I am not able to figure out yet. I have been trying to understand where not caching the controller (loaded in the non-default context) could cause issues, but I was able to find it been called during the initial app request or when the collection is changed and notified by the |
Has anyone been able to find all caches that reference the assembly? |
Thanks for contacting us. |
Describe the bug
When you load an assembly in a new AssemblyLoadContext and add it dynamically via an AssemblyPart to the ApplicationPartManager new entries are getting added to the internal property caches. These entries do not get removed when the AssemblyPart is removed and the assembly is unloaded.
These are the caches that i am talking about:
PropertiesCache
VisiblePropertiesCache.
Because same types loaded in a different AssemblyLoadContext are not reference equal more and more entries are getting added.
It would be counterproductive to clear both dictionaries. At least the entries belonging to the unloaded assembly should be removed. Although i guess that the performance impact of clearing the whole cache wouldn't be high.
If you load and unload dlls multiple times you can clearly see the issue.
Without clearing the property caches:
With clearing the property caches:
To Reproduce
I added a repo that can be used for investigation: Link
If line 111 in AssemblyController is commented you experience a memory leak. If you trigger this method multiple times you can see in the returned result, that for every load an entry gets added to the PropertiesCache. If you comment this line and call this method in a production build you can see a significant increase in memory consumption. I did not experience an increase of the number of entries in the VisiblePropertiesCache, but I'm pretty sure both caches should be cleaned up if they contain an entry for an unloaded assembly.
Further technical details
Version 16.5.0 Preview 1.0
The text was updated successfully, but these errors were encountered: