问题
I have a static method that invokes WMI management objects and serializes the result. It works well enough, but it leaks memory like a sieve. About 20k every time I run it. (even with all of the serialization turned off).
I've tested in .Net 2.0, 3.0, 3.5 and the memory leak is constant throughout.
Am I doing something wrong? Or is there a way to box this method so that what ever leak it has, is thrown out when it finishes. (like boxing it in a child process)
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
try
{
using(ManagementObjectCollection moList = searcher.Get()){
foreach (ManagementObject mo in moList)
{
try
{
// Do serialization here. Return object will be a List<GenereicObjectType>
}
catch (Exception ex)
{
Debug.Error(ex);
}
finally
{
if (mo != null)
{
try {
mo.Dispose();
}
catch { }
}
}
}
}
}
catch (Exception ex)
{
Debug.Error(ex);
}
}
来源:https://stackoverflow.com/questions/8922601/wmi-memory-leak-solution