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.

1

4 Answers 4

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();
7
  • 2
    this is the true answer Jul 30, 2015 at 6:40
  • 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 Nov 24, 2015 at 15:41
  • 9
    That memsize is in kB not MB
    – Markus
    Nov 24, 2015 at 15:54
  • 2
    @laz-nikolaj-andersen please add a link reference to you answer Sep 1, 2017 at 3:53
  • 2
    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, 2022 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."

2
  • 5
    Watch out for locked pages though, which are not part of the working set (oddly enough), and the standard task manager doesn't see them either (even though they are most certainly consumed :D) Apr 13, 2011 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 3, 2016 at 2: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 !

1
  • Did you call Process.Refresh() before getting Process.PrivateMemorySize64?
    – Mr. Ree
    Jan 17, 2021 at 7:30
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

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.