title: How to measure memory usage with C# (as we can do in Java)?
body: <p>I'm trying to measure, in a C# programm, the memory usage.</p>
  
  <p>I'd like to know the C# equivalent of this Java function :</p>
  
  <pre><code>ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getCommitted()
  </code></pre>
  
  <p>that represent the total memory <strong>allocated</strong> for the heap. I need this to know the total size of memory allocated for my C# programm.</p>
  
  <p>Then in Java I can get the used memory with :</p>
  
  <pre><code>ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed()
  </code></pre>
  
  <p>Currently I use this in C# to get used memory :</p>
  
  <pre><code>Process_Memory = MyProcess.PrivateMemorySize64;
  </code></pre>
  
  <p>But I'm not totally sure that it's the equivalent.</p>
  
  <p>So in resume how can I get the total allocated space for my C# application and the current use at a time <em>t</em>?</p>
  
  <p><strong><em>EDIT :</em></strong></p>
  
  <p>From the answers and further research I've determine this :</p>
  
  <p><strong><em>Current memory in use</em></strong></p>
  
  <pre><code>System.GC.GetTotalMemory(false);
  </code></pre>
  
  <blockquote>
    <p>Give the number of bytes currently allocated in managed memory. (<a href="http://msdn.microsoft.com/fr-fr/library/system.gc.gettotalmemory.aspx" rel="noreferrer">http://msdn.microsoft.com/fr-fr/library/system.gc.gettotalmemory.aspx</a>)</p>
  </blockquote>
  
  <p>This method return a low value and I'm pretty sure it's not really a representation of all memory in use as I can get with <code>getUsed()</code> in <em>Java</em>. In java for the same application used memory start at 5MB and go to 125MB max. With C# I get from 1 to 5 MB with above method.</p>
  
  <p>It seems that :</p>
  
  <pre><code>MyProcess.PrivateMemorySize64; // return ~=25MB
  </code></pre>
  
  <p>or </p>
  
  <pre><code>MyProcess.WorkingSet64;  //return ~=20MB
  </code></pre>
  
  <p>Give a more accurate value of all the memory in use. But I don't know wich one I should use...</p>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <p>For the <strong><em>global allocated memory</em></strong> <a href="http://blogs.msdn.com/b/salvapatuel/archive/2007/10/13/memory-working-set-explored.aspx" rel="noreferrer">this article</a> suggests to use :</p>
  
  <pre><code>Process_MemoryEnd1 = MyProcess.VirtualMemorySize64;
  </code></pre>
  
  <p>It return <strong><em>always the same value</em></strong> along the programm, about 169MB that seems fair compared to Java : from 64MB to 170MB max</p>
  
  <p>I'm still looking for an accurate answer all I found is very vague and I'm not very familiar with Windows memory management, I'm not really sure to undertand the docs I found :/</p>