Memory leak in WMI

B

beattie.stuart

I think I've found a memory leak trying to use the
system.management.ManagementObject, but it could be my programming
skills so I'd appreciate some advice. I've writing a monitoring routine
that queries WMI for some stats, but it continually eats into memory.
I've cut the code down to the following sample but the problem is still
there:

Imports System
Imports System.Management

Module Module1
Private mObjectCpu As ManagementObject

Sub Main()
Dim Counter As Int32
While Counter < 10000
mObjectCpu = New
ManagementObject("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
mObjectCpu.Get()
mObjectCpu.Dispose()
Counter += 1
End While
End Sub

End Module

I've used the .Net Memory Profiler from Scitech and it's showing a load
of undisposed instances of the object
System.Management.IWbemClassObjectFreeThreaded, but I don't have
references to it in my code, and I can't find anything about it in
MSDN.
Does anyone have any ideas what I've done wrong, or has anyone seen
this kind of problem before?

Cheers,
Stu
 




| Imports System
| Imports System.Management
|
| Module Module1
| Private mObjectCpu As ManagementObject
|
| Sub Main()
| Dim Counter As Int32
| While Counter < 10000
| mObjectCpu = New
| ManagementObject("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
| mObjectCpu.Get()
| mObjectCpu.Dispose()
| Counter += 1
| End While
| End Sub
|
| End Module

will this work as well if you create the mobjectcpu outside of the while
loop and simply .get() w/n the loop and finally dispose it as you exit the
sub main? what is it that this function is trying to accomplish?

that may not address the memory leak...but it may slow it to a trickle.

hth,

me
 
G

Guest

Try and dispose the management object after use inside the loop. You are
basically creating objects in a loop, which means 10000 objects will be
created and these will be deallocated only when the GC next runs. A dispose
call will atleast free up the unmanaged resources that are there.

I would expect the memory to go up till some threshold triggers teh GC. My
guess is that a dispose call may clear your problem.
 
S

Stu

Sorry for not mentioning it, but I'd had a look at moving the creation
and disposal of the ManagementObject outside of the loop and it doesn't
have any affect. It appears to the be when I call the Get method, under
the hood it carries out some call to unmanaged code and creates some
sort of object (the IWbemClassOb­jectFreeThreaded) which it then
doesn't dispose of properly.
I'm getting this information from the Memory Profiler app, and it's
showing that in the original example the ManagementObject was being
created and disposed of properly. If it helps anyone (cos this is
getting out of my depth) it shows the instance of the
IWbemClassOb­jectFreeThreaded has a call stack of:
MarshalWbemObject.MarshalNativeToManaged(IntPtr)
ManagementObject.Get()
Module1.Main()
And this is the same for each of the several throusand instances of
IWbemClassOb­jectFreeThreaded.
I'm thinking if I could get some sort of handle on that object I could
try and dispose of it myself, but I'm not sure where to start?

Cheers,
Stu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top