Performance counter values are cleared after object disposal

Sep 19, 2012 at 6:35 PM


I'm just trying your library with the examples provided into the documentation page.

/// <summary>
/// Logs the user.
/// </summary>
public void LogUser()
    if (!IsEnabled) return;
    using (CounterHelper<MultipleInstancePerformanceCounters>  counterHelper = PerformanceHelper.CreateCounterHelper<MultipleInstancePerformanceCounters>())

The problem is that there is no values recorded into the counter when i look through the performance monitor (perfmon.msc).

After some debugging, I was able to keep track of values avoiding the object disposal of your examples. (removing the "using" block).

What is your advice to use this library? Currently, I'm using a custom singleton class to invoke my counters, this class should be instantiated and also disposable?

Thanks in advance for your advice.

Sep 21, 2012 at 1:38 AM

Any Disposable Object should be explicity dispose when you no longer need it. Since the system object "PerformanceCounter" is Disposable, I had to make any wrapper or container Disposable too.


If you are going to wrapper it inside a Single object, you will need to be sure to dispose it when application is shutting doing explicity, otherwise you may leave dirty values on the system counters.

Mar 20, 2013 at 6:19 PM
Performance Counters are all based on the Shared Memory model and make use of the Reader and Writer pattern. What this means is that the counter instance will not actuall exist before or after its lifetime. If there is no one reading the the value when it exists, the shared memory of it will evaporate when its lifetime ends. As long as something is holding a handle to the shared memory, the counter can be read from or writen to. In addition Performance Monitor does not record the values of performance counters being viewed unless it is running a data collector (defined in the Data Collector Sets) that includes the specific counter.