title: How to use Performance Counter or Process class correctly in C# to get memory usage of current process?
body: <p>According to <a href="https://stackoverflow.com/questions/3411805/how-to-use-net-performancecounter-to-track-memory-and-cpu-usage-per-process">How to use .NET PerformanceCounter to track memory and CPU usage per process?</a> <code>PerformanceCounter</code> should give me the number of memory usage of a given process.</p>
  
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/ccf1tfx0.aspx" rel="nofollow noreferrer">MSDN</a>, <code>Process</code> instance may also give me more or less the same number.</p>
  
  <p>In order to verify my assumptions, I wrote the following code:</p>
  
  <pre><code>class Program
  {
      static Process process = Process.GetCurrentProcess();
  
      static PerformanceCounter privateBytesCounter = new PerformanceCounter("Process", "Private Bytes", process.ProcessName);
      static PerformanceCounter workingSetCounter = new PerformanceCounter("Process", "Working Set", process.ProcessName);
  
      static void Main(string[] args)
      {
  
  
          GetMeasure();
  
          Console.WriteLine("\nPress enter to allocate great amount of memory");
          Console.ReadLine();
          int[] arr = new int[10000000];
          for (int i = 0; i &lt; arr.Length; i++)
          {
              arr[i] = i;
          }
  
          GetMeasure();
  
          privateBytesCounter.Dispose();
          workingSetCounter.Dispose();
          Console.ReadKey();
      }
  
      private static void GetMeasure()
      {
          Console.WriteLine("{0,38} {1,20}", "Private bytes", "working set");
          Console.WriteLine("process data{0,23} {1,20}", process.PrivateMemorySize64 / 1024, process.WorkingSet64 / 1024);
          Console.WriteLine("PerformanceCounter data{0,12} {1,20}", privateBytesCounter.NextValue() / 1024, workingSetCounter.NextValue() / 1024);
      }
  
  }
  </code></pre>
  
  <p>The output looks like</p>
  
  <pre><code>                         Private bytes          working set
  process data                  22880                17516
  PerformanceCounter data       21608                15608
  
  Press enter to allocate great amount of memory
  
                           Private bytes          working set
  process data                  22880                17516
  PerformanceCounter data       21608                15608
  </code></pre>
  
  <p>Exactly the same! In the contrast, private bytes shown in Process Explorer increased from 32732 to 63620.</p>
  
  <p>So am I doing something wrong?</p>