views:

29

answers:

2

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?

A: 

Below points may be of some help

1-

Can ManagementBase class be singleton? if yes than you can reduce overhead of creating unnessary objects of this class?

2- You can implement IDisposible interface on ManagementBase which can free IWbemClassObjectFreeThreaded objects?

3- You can also implement a destructor on ManagementBase class which can also free objects. But it may result in some performance degradation.

saurabh
+2  A: 

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

Hans Passant
This is pretty close to what I've discovered the problem to be. See link http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/158d5f4b-1238-4854-a66c-b51e37550c52/ . It seems like because the ManagementBaseObject isn't created from within the program, the gc doesn't recognize it as needing cleanup. I had to call dispose manually on all Management type classes.
Jeremy
Hehe, that's me. I remembered it, couldn't think of the search terms to find it back.
Hans Passant