C# PerformanceCounter Class causes Handle Leak

1.2k views Asked by At

I have an application developed with .NET 4.0. This application keeps track on some Custom Performance Counter and display to the user. Recently i found that there's handle leak in the application. The 2 types of handles are Mutant and PcwObject.

I followed this page (http://blogs.technet.com/b/yongrhee/archive/2011/12/19/how-to-troubleshoot-a-handle-leak.aspx) and got the following stack trace:

Handle = 0x0000000000003760 - OPEN
Thread ID = 0x00000000000073d0, Process ID = 0x0000000000005fdc

0x0000000077c41cea: ntdll!NtCreateMutant+0x000000000000000a
0x000007fefde08bf7: KERNELBASE!CreateMutexExW+0x000000000000004f
0x000007fefde14460: KERNELBASE!CreateMutexExA+0x0000000000000038
0x000007feff6bbcf6: ADVAPI32!PerflibciOpenLocalQueryHandle+0x0000000000000116
0x000007feff6a5a86: ADVAPI32!PerflibciQueryV2Provider+0x000000000000020d
0x000007feff68926d: ADVAPI32!QueryExtensibleData+0x00000000000004a2
0x000007feff6898e4: ADVAPI32!alloca_probe+0x00000000000051b2
0x00000000779d4087: KERNEL32!TlsGetValue+0x000000000000fbb8
0x00000000779e4b52: KERNEL32!RegQueryValueExW+0x00000000000000f2
0x000007feff68c2ed: ADVAPI32!RegQueryValueExWStub+0x000000000000001d
0x000007fef99b17c7: clr!DoNDirectCall__PatchGetThreadCall+0x000000000000007b
0x000007fef8a38422: mscorlib_ni+0x0000000000428422
0x000007fef89948f1: mscorlib_ni+0x00000000003848f1
0x000007fef899392e: mscorlib_ni+0x000000000038392e

and

Handle = 0x0000000000003998 - OPEN
Thread ID = 0x0000000000002808, Process ID = 0x0000000000005fdc

0x0000000077c4138a: ntdll!NtDeviceIoControlFile+0x000000000000000a
0x000007fefce214a3: pcwum!PcwpSendIoctl+0x00000000000000f3
0x000007fefce21962: pcwum!PcwCreateNotifier+0x000000000000003e
0x000007feff6abf53: ADVAPI32!PerfpCreateProvider+0x00000000000000d3
0x000007feff6ddb77: ADVAPI32!PerflibciLocalValidateCounters+0x0000000000000167
0x000007feff6a5ced: ADVAPI32!PerflibciQueryV2Provider+0x0000000000000478
0x000007feff68926d: ADVAPI32!QueryExtensibleData+0x00000000000004a2
0x000007feff6898e4: ADVAPI32!alloca_probe+0x00000000000051b2
0x00000000779d4087: KERNEL32!TlsGetValue+0x000000000000fbb8

I also opened Process Explorer to monitor the handle state. According to my observation, the above 2 handles (3760 and 3998) keep alive for over half an hour and not yet destroyed. The handles count is increased by ~1000 within 2 hours. half of them are Mutant and the other half are PcwObject.

I suspect it is related to PerformanceCounter coz i know the PerformanceCounter Class grep data from Registry and i find PerflibciQuery and RegQueryValue in the stack trace.

I've search through the Internet but seems no luck. Does anyone have any idea about this? Thanks

Additional Information

I tested those Performance Counters one by one and find that these handles were leaked when getting this counter : PerformanceCounter("HTTP Service Request Queues", "CurrentQueueSize", "ABC")

My Code is like this :

private PerformanceCounter counter;

private void Detect()
{
    /*
    Do sth
    */

    try
    {
        if (null == counter) counter = new PerformanceCounter("HTTP Service Request Queues", "CurrentQueueSize", "ABC");
        long rawValue = counter.RawValue;
        if (0 < rawValue)
            WriteLog("ABC CurrentQueueSize: {0}", rawValue);
    }
    catch(Exception e)
    {
        WriteLog("Fail to get ABC counter. {0}", e);
    }

}

counter is a member variable and I'm very sure that it is disposed when this class is destroyed. So i don't know why it leaks the handles.

2

There are 2 answers

0
adrian_sll On BEST ANSWER

I already found the cause few days ago but forgot to post it. Sorry about this.

Actually this is a resource leak caused by using v2 PerformanceCounter (http://support.microsoft.com/kb/2734909). Then I followed instruction in this page (http://msdn.microsoft.com/en-us/library/aa392740(v=vs.85).aspx) and found that "HTTP Service Request Queues" is a v2 counters provider.

So that's the cause! Done!

1
Ivan Rebo On

What i noticed recently was a handle leak if you ask the counter for a value in a different thread. If you create a new thread, ask the performance counter for next value it will create bunch of handled that dont get cleaned up even if you dispose the counter.