Take the 2-minute tour ×

How to measure the total memory consumption of the current process programatically in .NET?

share|improve this question

4 Answers 4

up vote 20 down vote accepted

Refer to this SO question

Further try this

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
share|improve this answer
Accordingly to this blog blogs.msdn.com/salvapatuel/archive/2007/10/13/… Working set != Total process memory –  Jader Dias Feb 26 '10 at 14:00
But in my tests the WorkingSet64 value is very very very close to the one shown by the TaskManager –  Jader Dias Feb 26 '10 at 14:03
@Jader Dias - this answer captures the essence of what is required (use of the System.Diagnostics.Process type) but be wary of what the garbage collector may or may not be doing otherwise you may end up with highly misleading results - I show how to avoid this problem in my answer –  Adam Ralph Feb 26 '10 at 14:06

I have found this very useful:

var initialMemory = System.GC.GetTotalMemory(true);
// body
var somethingThatConsumesMemory = Enumerable.Range(0, 100000)
// end
var finalMemory = System.GC.GetTotalMemory(true);
var consumption = finalMemory - initialMemory;
share|improve this answer

If you only want to measure the increase in say, virtual memory usage, caused by some distinct operations you can use the following pattern:-


var before = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;

// performs operations here

var after = System.Diagnostics.Process.GetCurrentProcess().VirtualMemorySize64;

This is, of course, assuming that your application in not performing operations on other threads whilst the above operations are running.

You can replace VirtualMemorySize64 with whatever other metric you are interested in. Have a look at the System.Diagnostics.Process type to see what is available.

share|improve this answer

PerformanceCounter class -


There are several of them -


Here is the CLR memory counter -


share|improve this answer
I find this counters confusing. Example: "# Total reserved Bytes" : "Displays the amount of virtual memory. in bytes, currently reserved by the garbage collector." Then I wonder? Is this about the process memory or about the memory that will be collected soon? –  Jader Dias Feb 26 '10 at 14:06
new PerformanceCounter("Process", "Private Bytes", "ConsoleApplication1.vshost").RawValue looks promising –  Jader Dias Feb 26 '10 at 14:16

Your Answer


