内存泄漏。 ManagementBaseObject 保留为 GC Root,但从未清理过

发布于 2024-09-26 20:44:24 字数 299 浏览 2 评论 0原文

我正在使用 ANTS Memory Profiler 来尝试确定应用程序的内存使用量持续增长的原因。

我运行该应用程序并随着时间的推移拍摄各种快照。我可以看到 IWbemClassObjectFreeThreaded 和 ManagementBaseObject 的实时实例随着时间的推移不断增加。查看类引用资源管理器,我可以看到 IWbemClassObjectFreeThreaded 被 ManagementBaseObject 引用,并且 100% 的 ManagementBaseObject 都是 GC Root,但它们似乎从未被清理过。我还能什么时候做?

I am using ANTS Memory Profiler to try and determine why my application's memory usage is continuing to grow.

I run the application and take various snapshots over time. I can see that the live instances of IWbemClassObjectFreeThreaded and ManagementBaseObject keeps increasing over time. Looking at the class reference explorer I can see that IWbemClassObjectFreeThreaded is referenced by ManagementBaseObject, and 100% of ManagementBaseObjects are GC Roots, but they never seem to be cleaned up. When else can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

若无相欠,怎会相见 2024-10-03 20:44:25

这是一个不寻常的问题,但也有可能发生。 WMI 基于 COM,IWbemClassObject 是一个获取 RCW 包装器的 COM 接口。在终结器线程运行之前,这些包装器不会被清理。从技术上讲,运行大量 WMI 查询是可能的,但没有对结果进行足够的工作来让垃圾收集器运行。

使用性能监视器 Perfmon.exe 对此进行诊断。右键单击图表,添加计数器、.NET CLR 内存并添加 # Gen 0 Collections 计数器。从底部列表中选择您的程序。程序运行时观察计数器。如果它没有滴答作响,你就会遇到这个问题。

如果是这种情况,请检查您的代码并验证运行如此多的查询但从不或很少使用结果是否仍然有意义。解决方法是对它们进行计数,并每隔 100,000 次调用 GC.Collect() 和 GC.WaitForPendingFinalizers()。

This is an unusual problem but it can happen. WMI is COM based, the IWbemClassObject is a COM interface that gets an RCW wrapper. These wrappers don't get cleaned-up until the finalizer thread runs. It is technically possible to run a lot of WMI queries but not do enough work with the results to get the garbage collector to run.

Diagnose this with Perfmon.exe, Performance Monitor. Right click the graph, Add Counters, .NET CLR memory and add the # Gen 0 Collections counter. Select your program from the bottom list. Observe the counter while your program is running. You'll have this problem if it is not ticking up.

If this is the case, review your code and verify if it still makes sense to run so many queries but never or rarely use the results. A workaround is to count them and every, say, 100,000 times call GC.Collect() and GC.WaitForPendingFinalizers().

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