title: What is the right way to monitor application memory usage?
body: <p>For debugging purposes, I have written this little static method:</p>
  
  <pre><code>public static long CheckMemory(long maxMemorySizeBytes)
  {
      GC.Collect();
      GC.WaitForPendingFinalizers();
      GC.Collect();
  
      var usedMemoryBytes = Process.GetCurrentProcess().VirtualMemorySize64;
      if (usedMemoryBytes &gt; maxMemorySizeBytes)
          Debugger.Break();
  
      return usedMemoryBytes;
  }
  </code></pre>
  
  <p>For some reason, <code>VirtualMemorySize64</code> keeps returning lots more memory than what Visual Studio Diagnostic Tools window is showing, as well as what the Task Manager is showing. For the specific example I'm running now, here are the numbers:</p>
  
  <ul>
  <li>Diagnostic Tools: ~250 MB</li>
  <li>Task Manager: ~120 MB</li>
  <li>VirtualMemorySize64: ~1100 MB</li>
  </ul>
  
  <p>Why are there such large differences and how do I properly track memory usage from within the app itself?</p>