开发者

Memory leak. ManagementBaseObject stays as GC Root, but never cleaned up

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?


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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜