Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top