RE: PerformanceCounter handle leak

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Ryan Byington (ryanbyi_at_online.microsoft.com)
Date: 02/21/04

  • Next message: Jerry III: "Re: PerformanceCounter handle leak"
    Date: Sat, 21 Feb 2004 02:07:45 GMT
    
    

    Unless you have a verry compelling reason the pattern of creating a new
    PeformanceCounter just to call NextSample on it then destroying it should
    not be done. This is because creating a new PerformanceCounter is not a
    trivial ammount of work. Also some counter types require two CounterSamples
    to calculate the value and if you only have one CounterSample the
    calculated value will be zero for these counter types.

    I have tried to get the handle count to increase per new performance
    counter created and I have not been able to reproduce this. If you post
    some more repro steps and the version of the runtime that your are using I
    can look into this further for you.

    Ryan Byington [MS]

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm

    --------------------
    | From: "Jerry Pisk" <jerryiii@hotmail.com>
    | Subject: PerformanceCounter handle leak
    | Date: Tue, 20 Jan 2004 16:49:35 -0800
    | Lines: 21
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    | Message-ID: <uUdlxh73DHA.2656@tk2msftngp13.phx.gbl>
    | Newsgroups: microsoft.public.dotnet.framework.performance
    | NNTP-Posting-Host: 12.173.163.30
    | Path:
    cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
    phx.gbl!tk2msftngp13.phx.gbl
    | Xref: cpmsftngxa07.phx.gbl
    microsoft.public.dotnet.framework.performance:6441
    | X-Tomcat-NG: microsoft.public.dotnet.framework.performance
    |
    | I was wondering if someone came across a handle leak when using
    | PerformanceCounter class. My code comes down to doing this:
    |
    | PerformanceCounter Counter = new PerformanceCounter("CategoryName",
    | "CounterName", "InstanceName");
    | CounterSample Sample = Counter.NextSample();
    | // Dump the sample values here
    | Counter.Close()
    |
    | Each time this runs about 10 handles are not released until the next
    garbage
    | collection. This is causing some serious problems since I'm collecting 10+
    | counters each time. Is there a way to release those handles as soon as I'm
    | done using the counter? I mean, my process is using upwards of 5,000
    handles
    | before GC kicks in and cleans them up... If I do not collect the counter
    | samples I'm running at less than 200 handles in the same process.
    |
    | Thanks
    |
    | Jerry Pisk
    |
    |
    |


  • Next message: Jerry III: "Re: PerformanceCounter handle leak"