.NET MethodInfo 缓存可以清除或禁用吗?
根据 MSDN,调用 Type.GetMethods()
将反映的方法信息存储在 MemberInfo
缓存中,因此不必再次执行昂贵的操作。
我有一个扫描程序集/类型的应用程序,寻找与给定规范匹配的方法。问题在于,由于 .NET 依赖于方法元数据,因此内存消耗显着增加(尤其是在引用大量程序集的情况下)。
有什么方法可以清除或禁用此 MemberInfo
缓存吗?
Per MSDN, calling Type.GetMethods()
stores reflected method information in a MemberInfo
cache so the expensive operation doesn't have to be performed again.
I have an application that scans assemblies/types, looking for methods that match a given specification. The problem is that memory consumption increases significantly (especially with large numbers of referenced assemblies) since .NET hangs onto the method metadata.
Is there any way to clear or disable this MemberInfo
cache?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不这么认为。一个技巧是在 AppDomain 中完成这项工作。您可以创建一个新的 AppDomain,完成所有工作,报告结果,然后卸载 AppDomain。这不是一个简单的任务,而且相当慢,但它是有效卸载程序集或反射相关缓存的唯一方法(据我所知)。
I don't think so. One trick would be to do this work in an AppDomain. You could create a new AppDomain, do all of your work, report your results, then unload the AppDomain. Not a trivial task and fairly slow but it is the only way to effectively unload assemblies or reflection related caches (that I know of).
如果不需要执行方法,可以使用 Assembly.ReflectionOnlyLoad(string)。然而,卸载程序集仍然需要卸载 AppDomain,因此,如果您的问题是泄漏(您的程序保持打开状态,用户不断向您传递新程序集以无限期地查看),而不仅仅是高内存使用率,那么这将无济于事。
You can reduce the memory consumption somewhat if you don't need to execute the methods by using Assembly.ReflectionOnlyLoad(string). Unloading the assemblies will still require unloading the AppDomain, however, so if your problem is a leak (your program stays open, user keeps passing you new assemblies to look at indefinitely), instead of just high memory use, this won't help.