71

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

4 Answers 4

68

Refer to this SO question

Further try this

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
3
  • 7
    Accordingly to this blog blogs.msdn.com/salvapatuel/archive/2007/10/13/… Working set != Total process memory
    – Jader Dias
    Feb 26, 2010 at 14:00
  • 1
    But in my tests the WorkingSet64 value is very very very close to the one shown by the TaskManager
    – Jader Dias
    Feb 26, 2010 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, 2010 at 14:06
43

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

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

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.

2
  • 2
    Why there is need to call GC.Collect two times?
    – Viru
    Jan 22, 2016 at 9:33
  • 8
    The first time, objects are put on the freachable queue and are later finalized. Afterwards, they are collectable.
    – Adam Ralph
    Jan 22, 2016 at 15:55
4

PerformanceCounter class -

http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

There are several of them -

http://msdn.microsoft.com/en-us/library/w8f5kw2e.aspx

Here is the CLR memory counter -

http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx

2
  • 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, 2010 at 14:06
  • 1
    new PerformanceCounter("Process", "Private Bytes", "ConsoleApplication1.vshost").RawValue looks promising
    – Jader Dias
    Feb 26, 2010 at 14:16
0

I have found this very useful:

Thread.MemoryBarrier();
var initialMemory = System.GC.GetTotalMemory(true);
// body
var somethingThatConsumesMemory = Enumerable.Range(0, 100000)
    .ToArray();
// end
Thread.MemoryBarrier();
var finalMemory = System.GC.GetTotalMemory(true);
var consumption = finalMemory - initialMemory;
1
  • 19
    Sigh... Just so anyone reading this isn't confused...You only provided code for one thread, so the MemoryBarrier calls are beyond useless (and there's no multi-threaded version of this where those would make any sense, either). I'm not sure what you think those do, but it's not what they actually do. Feb 5, 2015 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.