Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to dispose every ManagementObject?

Tags:

c#

wmi

I noticed that ManagementObject is IDisposable, but it's also returned from ManagementClass.GetInstances() and ManagementObjectSearcher.Get(), does this mean I need to dispose each object encountered?

Like so:

ManagementObject ret;
foreach(ManagementObject mo in searcher.Get()) {
    if( IsWhatIWant(mo) ) ret = mo;
    else mo.Dispose();
}

Further confounding this: there's a bug in ManagementBaseObject where it does not correctly implement IDisposable (See Using clause fails to call Dispose? ) so you need to call it yourself, or use a wrapper around it that does correctly call it.

This is irritating because I have so many ManagementObjectCollections around.

like image 976
Dai Avatar asked Mar 29 '13 05:03

Dai


2 Answers

I've created a helper object to dispose all created management objects:

public class ManagementObjectDisposer : IDisposable
{
    private List<IDisposable> disposables = new List<IDisposable>();

    /// <summary>
    /// Workaround to dispose ManagementBaseObject properly.
    /// See http://stackoverflow.com/questions/11896282
    /// </summary>
    /// <param name="disposable"></param>
    public static void DisposeOne(IDisposable disposable)
    {
        ManagementBaseObject mbo = disposable as ManagementBaseObject;
        if (mbo != null)
            mbo.Dispose();
        else
            disposable.Dispose();
    }

    public void Dispose()
    {
        Exception firstException = null;
        foreach (IDisposable d in Enumerable.Reverse(disposables))
        {
            try
            {
                DisposeOne(d);
            }
            catch (Exception ex)
            {
                if (firstException == null)
                    firstException = ex;
                else
                    cvtLogger.GetLogger(this).Error($"Swallowing exception when disposing: {d.GetType()}", ex);
            }
        }
        disposables.Clear();
        if (firstException != null)
            throw firstException;
    }

    public T Add<T>(T disposable) where T : IDisposable
    {
        disposables.Add(disposable);
        return disposable;
    }

    /// <summary>
    /// Helper for ManagementObjectSearcher with adding all objects to the disposables.
    /// </summary>
    /// <param name="query">The query string.</param>
    public IEnumerable<ManagementBaseObject> Search(string query)
    {
        ManagementObjectSearcher searcher = this.Add(new ManagementObjectSearcher(query));
        return EnumerateCollection(searcher.Get());
    }

    /// <summary>
    /// Helper for adding ManagementObjectCollection and enumerating it.
    /// </summary>
    public IEnumerable<ManagementBaseObject> EnumerateCollection(ManagementObjectCollection collection)
    {
        this.Add(collection);
        ManagementObjectCollection.ManagementObjectEnumerator enumerator = this.Add(collection.GetEnumerator());
        while (enumerator.MoveNext())
            yield return this.Add(enumerator.Current);
    }
}

Just use it like:

using (var moDisposer = new ManagementObjectDisposer())
{
    foreach (var mobj = moDisposer.Search("SELECT * FROM Win32_Processor")
        Console.WriteLine(mobj["DeviceID"]);
}

Note: the ManagementClass.GetInstances() is easy to add to the ManagementObjectDisposer, too.

like image 190
xmedeko Avatar answered Sep 23 '22 19:09

xmedeko


This is irritating because I have so many ManagementObjectCollections around.

Which has nothing to do with calling Dispose(), that only releases the underlying unmanaged COM objects. ManagementObjectCollection is a managed class, instances of it are garbage collected. Which is automatic, you can only help by calling GC.Collect(). Your program is probably just creating a lot of System.Management objects, possibly because that's the only thing it ever does. The quoted bug was fixed in the current versions of .NET 3.5SP1 and .NET 4.5 that I have installed on my machine.

So if you don't have a patched version of .NET then you don't only see a lot of System.Management objects on the GC heap, your process will also consume a lot of unmanaged memory. If the garbage collector doesn't run frequently enough then that can cause the program to crash with OOM. You didn't mention that as a failure mode so it is not strongly indicated that you have a real problem.

The initial size of generation 0 of the GC heap is 2 megabytes, it can grow to 8+ megabytes. That is a lot of ManagementObjectCollections objects, it is a very small object that takes only 24 bytes in 32-bit mode. The actual collection is unmanaged. Use Perfmon.exe or your memory profiler to check that the garbage collector is running frequently enough. If it doesn't then keep an eye on your program's VM size. If that's ballooning then using a counter in your query loop and calling GC.Collect() when it is high enough is a viable workaround. Careful with the info you get out of a memory profiler, it can irritate for the wrong reasons.

like image 45
Hans Passant Avatar answered Sep 22 '22 19:09

Hans Passant