.NET MethodInfo 缓存可以清除或禁用吗?

发布于 2024-09-05 18:32:16 字数 343 浏览 5 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2024-09-12 18:32:16

我不这么认为。一个技巧是在 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).

豆芽 2024-09-12 18:32:16

如果不需要执行方法,可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文