Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get memory usage as reported by windows 8 task manager

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

like image 280
PRinCEKtd Avatar asked Feb 06 '13 09:02

PRinCEKtd


1 Answers

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.

like image 86
usr Avatar answered Nov 14 '22 23:11

usr