How do I get the total memory used by my C# application? I have tried: GC.GetTotalMemory() Process.GetCurrentProcess().PrivateMemorySize, PrivateMemorySize64, VirtualMemorySize, VMS64, PagedMS.
They do report various values, which is OK. But these values differ from what I see in the Task Manager.
I am using Windows 8 and the task manager shows resources used by all apps separately on the right hand side besides the application name. My Application's current memory is also displayed. Suppose it is 18.9 MB, and drops down to say ~9 MB if I force a GC.Collect().
Now, before GC.Collect(): PrivateMemorySize & PMS64 both report ~42 (after converting to MB) and GetTotalMemory() gives ~3 PagedMS & PagedMS64 gives ~379
and after GC.Collect(): PrivateMemorySize & PMS64 both report ~39 (after converting to MB) and GetTotalMemory() gives ~1 PagedMS & PagedMS64 gives ~367
But these are different from task manager's display. What I want to get is the '18.9' and '9' ie. the memory usage of my application as reported by the windows 8 task manager. How do I do this through code behind? I use .Net 4.0, visual studio 2012
I'd not try to replicate what taskmgr does but use the number that logically makes sense for you. Probably, that's private bytes (of course, use the 64 bit version). Private bytes are those bytes that are actually in use and that are not shared with any other process.
There is no memory number that is perfect (neither on Windows nor on Linux) because memory usage is open to interpretation as soon as multiple processes share some pages.
Private bytes is very appropriate for most apps, though.
Update: The Windows 8 Task Manager shows Working Set Private Bytes. This is an awful metric to use. It tells you how much memory of this app is currently in RAM. This can be much less than actual memory usage. It does not tell you how much memory will be freed if this process exits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With