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

atikot picture atikot · Dec 31, 2014 · Viewed 11.3k times · Source

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)

Answer

Sriram Sakthivel picture Sriram Sakthivel · Dec 31, 2014

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);