Search code examples
c#processramconsumption

Get real process RAM usage


I've been making some research of how to get a process RAM usage by id, but all of the results lead to the use of WorkingSet64 variable, which returns an incorrect value. I have already compared both values WorkingSet64 and the value in the Task Manager, and they aren't equal.

I would like to know how to get the real and correct process RAM usage, just like the Windows' Task Manager's one.

Can you help me out? Thanks in advance.


Solution

  • In windows 8.1, the default memory column in task manager is the Private Working Set.

    See this question to see how to obtain such value.

    To summarize from the referenced question's answer:

    Use the PerformanceCounter class to obtain the "Working Set - Private" counter from the "Process" category.

    Example:

    var counter = new PerformanceCounter("Process", "Working Set - Private", prcName);
    

    Where prcName is the name of the process. For example, to get the name of the current process, use the following:

    string prcName = Process.GetCurrentProcess().ProcessName;
    

    If you want to get the name for a process given its id, use the following:

    string prcName = Process.GetProcessById(process_id).ProcessName;