32

I have been having some trouble figuring out how exactly I get a process's ram usage. (How much ram it is currently consuming, not how much is reserved, or its max or min)

Lets say I have a process running in the back ground, Java.exe, it is allowed to use 1024mb of ram, how can I tell how much ram it is currently using.

I am starting the process myself, so I have access to the Process object, I would just like a little more clarification on what property is the one for me.

Meiscooldude
  • 3,671
  • 5
  • 27
  • 30
  • Possible duplicate of [How to get memory available or used in C#](https://stackoverflow.com/questions/750574/how-to-get-memory-available-or-used-in-c-sharp) – Saul Jun 08 '17 at 15:44

4 Answers4

56

I found this on msdn and it is working

System.Diagnostics.Process proc = ...; // assign your process here :-)

int memsize = 0; // memsize in KB
PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "Working Set - Private";
PC.InstanceName = proc.ProcessName;
memsize = Convert.ToInt32(PC.NextValue()) / (int)(1024);
PC.Close();
PC.Dispose();
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 2
    this is the true answer – Behzad Hassani Jul 30 '15 at 06:40
  • 1
    I don't think you need to cast the integer do you? – Codingale Aug 11 '15 at 21:36
  • @Laz tested and the returned value is the most accurate compared with Task Manager. but for some multithreaded process like google chrome, it unable to get accurate results, any ideas? – Dennis Sep 10 '15 at 08:12
  • 4
    @Dennis - probably because chrome is not only multi threaded but also multi process (multiple executables of the same name, running at one time). So it only returns the memory of any one of the chrome executables. you can double check this using Task Manager – Robin Rodricks Nov 24 '15 at 15:41
  • 9
    That memsize is in kB not MB – Markus Nov 24 '15 at 15:54
  • 2
    @laz-nikolaj-andersen please add a link reference to you answer – Akira Yamamoto Sep 01 '17 at 03:53
  • 1
    People should be aware that this works off of process name, which means if you have multiple instances of one exe then this may not return the correct value. – Ian Newson Apr 19 '22 at 13:06
21

If you are purely interested in physical memory, you probably want WorkingSet64, which gives "the amount of physical memory allocated for the associated process." Understand that this value constantly fluctuates, and the value this call gives you may not be up to date. You may also be interested in PeakWorkingSet64, which gives "the maximum amount of physical memory used by the associated process."

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 5
    Watch out for [locked pages](http://forum.sysinternals.com/topic23886_post122660.html) though, which are not part of the working set (oddly enough), and the [standard task manager doesn't see them either](http://blogs.msdn.com/b/psssql/archive/2009/09/11/fun-with-locked-pages-awe-task-manager-and-the-working-set.aspx) (even though they are most certainly consumed :D) – Roman Starkov Apr 13 '11 at 12:19
  • I want to use PeakWorkingSet64 to get the max memory used by process, but I don't know when to get this property to give the most accuracy value. I think the best moment to get the property is when the process is "OnExitting", but Process class don' have this event. It's just have Exited event – Andiana Feb 03 '16 at 02:13
2

great, I wanted this to get the same as depicted in task manager,and tried:

Process.PrivateMemorySize64
Process.PeakVirtualMemorySize64
Process.PeakPagedMemorySize
Process.PagedSystemMemorySize64
Process.PagedMemorySize64
Process.NonpagedSystemMemorySize64
Process.WorkingSet64

and none of those worked but Performance Counter does !

PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "Working Set - Private";
PC.InstanceName = "processNameHere";
memsize = Convert.ToInt32(PC.NextValue()) / (int)(1024);
PC.Close();
PC.Dispose();

thanks a lot !

Mau Mills
  • 21
  • 3
0

Win32 GetSystemInfo function works, too.

It is actually successfully used on practice for array of bytes scanning in the process here in this Memory.dll project: https://github.com/erfg12/memory.dll/blob/042db0cf75e4152a7adf1ea47e6f23f1ad763fb6/Memory/memory.cs#L1909

KulaGGin
  • 943
  • 2
  • 12
  • 27