How to get application memory usage as shown in Task Manager?

14,220

Solution 1

Presumably you're looking at the wrong column in "Task manager" or using the wrong property in Process class..

I guess you're looking for WorkingSet64 not PrivateMemorySize64. PrivateMemorySize64 is the amount of virtual memory allocated for the process, not the physical memory. For physical memory use WorkingSet64.

Also, you need to call process.Refresh() before accessing any of the dynamic properties in process class as it is heavily cached.

process.Refresh();
_data.MemoryUsed = (process.WorkingSet64).ConvertBytesToMegabytes().ToString(CultureInfo.InvariantCulture);

Solution 2

Try once with the following code,may be it will help

PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "ServerProcess";
PC.CounterName = "Working Set - Private";
PC.InstanceName = JSP[0].ProcessName; //Process
RAM_memorysize = PC.NextValue();            //float RAM_memorysize;
PC.Close();
PC.Dispose();

Solution 3

None of the above is working for me. I have therefore found this solution

How to get application memory usage as shown in Task Manager?

by Hans Passant which is doing a great job.

string prcName = Process.GetCurrentProcess().ProcessName;
var counter_Exec = new PerformanceCounter("Process", "Working Set - Private", prcName);
double dWsp_Exec =  (double)counter_Exec.RawValue / 1024.0; <---that is the value in KB

Also, I sometimesfound a minor different (<10%) which could be related to a different update. enter image description here

The only minor drawback is that the first instruction take a long time (5") for accumulating data so it might be handled in a thread.

Share:
14,220
atikot
Author by

atikot

Updated on July 25, 2022

Comments

  • atikot
    atikot almost 2 years

    I am trying to get the memory usage of my application but for some reason i am getting different size than in task manager

    I am using:

    enter image description here

    Task manager shows that my application occupies 45mb , while when i pulling it in my code i get 85mb how can i get the same size as in task manager (without using wmi)

  • atikot
    atikot over 9 years
    i added refresh and uploaded the code while debugging as you see the memory size is different, also there are not drastic changes in memory size it's pretty stable at 45-47mb~
  • Sriram Sakthivel
    Sriram Sakthivel over 9 years
    Don't check the process memory while debugging. Run the process out of the debugger and check. Because when debugging visual studio will run hosting process for you YourProcess.vshost.exe. That can cause confusion. I tested this with printing the output in console (running out of visual studio) and that worked perfectly. Also change that Sleep(200) to Sleep(1000) so that it will sync with task manager.
  • atikot
    atikot over 9 years
    I am trying without visual studio and debug and it is still not right, i don't see the vshost in task manager also
  • Sriram Sakthivel
    Sriram Sakthivel over 9 years
    As I said, you're still looking at the wrong column in the task manager. You need to check the "Working set" column. If you need private working set check this answer
  • atikot
    atikot over 9 years
    also WorkingSet64 shows 114mb, PrivateMemorySize64 shows 80-84mb
  • atikot
    atikot over 9 years
    is there a way to get it from Process without using Performence counters or wmi?
  • Sriram Sakthivel
    Sriram Sakthivel over 9 years
    @atikot Sorry, I don't know alternate way. What's wrong with performance counter anyway?
  • Chris O
    Chris O over 9 years
    @atikot You could also go directly to the WinAPI and P/Invoke GetProcessMemoryInfo