title: How to solve memory leak caused by System.Diagnostics.PerformanceCounter
body: <h3>Summary</h3>
  
  <p>I have written a process monitor command-line application that takes as parameters:<br/></p>
  
  <ul>
  <li>The process name or process ID</li>
  <li>A CPU Threshold percent.</li>
  </ul>
  
  <p>What the program does, is watches all processes with the passed name or pid, and if their CPU usage gets over the threshold%, it kills them.<br/></p>
  
  <p>I have two classes:
  <code>ProcessMonitor</code> and <code>ProcessMonitorList</code><br/></p>
  
  <p>The former, wraps around <code>System.Diagnostics.PerformanceCounter</code><br/>
  The latter is an <code>IEnumarable</code> that allows a list-like structure of the former.<br/></p>
  
  <h3>The problem</h3>
  
  <p>The program itself works fine, however if I watch the Memory Usage on Task Manager, it grows in increments of about 20kB per second. Note: the program polls the CPU counter through <code>PerformanceCounter</code> every second.</p>
  
  <p>This program needs to be running on a heavily used server, and there are a great number of processes it is watching. (20-30).</p>
  
  <h3>Investigation So far</h3>
  
  <p>I have used PerfMon to monitor the <strong>Private Bytes</strong> of the process versus the <strong>Total number of Bytes in all Heaps</strong> and according to the logic presented in the article referenced below, my results indicate that while fluctuating, the value remains bounded within an acceptable range, and hence there is no memory leak:<br/>
  <a href="http://www.dotnetfunda.com/articles/article625-best-practices-no-5--detecting-net-application-memory-leaks-.aspx#Avoidtaskmanagertodetectmemoryleak" rel="nofollow noreferrer">Article</a><br/></p>
  
  <p>I have also used FxCop to analyze my code, and it did not come up with anything relevant.</p>
  
  <h3>The Plot Thickens</h3>
  
  <p>Not being comfortable with just saying, <em>Oh then there's no memory leak</em>, I investigated further, and found (through debugging) that the following lines of code demonstrate where the leak is occurring, with the arrow showing the exact line.</p>
  
  <pre><code>_pc = new PerformanceCounter("Process", "% Processor Time", processName);
  </code></pre>
  
  <p>The above is where _pc is initiated, and is in the constructor of my <code>ProcessMonitor</code> class.</p>
  
  <p>The below is the method that is causing the memory leak. This method is being called every second from my main.</p>
  
  <pre><code>public float NextValue()
  {
          if (HasExited()) return PROCESS_ENDED;
          if (_pc != null)
          {
              _lastSample = _pc.NextValue();   //&lt;-----------------------
              return _lastSample;
          }
          else return -1;
  }
  </code></pre>
  
  <p>This indicates to me that the leak exists inside the <code>NextValue()</code> method, which is inside the System.Diagnostics.PerformanceCounter class.</p>
  
  <h3>My Questions:</h3>
  
  <ol>
  <li>Is this a known problem, and how do I get around it?</li>
  <li>Is my assumption that the task manager's memory usage increasing implies that there is indeed a memory leak correct?</li>
  <li>Are there any better ways to monitor multiple instances of a specific process and shut them down if they go over a specific threshold CPU usage, and then send an email?</li>
  </ol>