views:

1321

answers:

5

I'm trying to find out how much memory my own .Net server process is using (for monitoring and logging purposes).

I'm using:

Process.GetCurrentProcess().PrivateMemorySize64

However, the Process object has several different properties that let me read the memory space used: Paged, NonPaged, PagedSystem, NonPagedSystem, Private, Virtual, WorkingSet

and then the "peaks": which i'm guessing just store the maximum values these last ones ever took.

Reading through the MSDN definition of each property hasn't proved too helpful for me. I have to admit my knowledge regarding how memory is managed (as far as paging and virtual goes) is very limited.

So my question is obviously "which one should I use?", and I know the answer is "it depends".

This process will basically hold a bunch of lists in memory of things that are going on, while other processes communicate with it and query it for stuff. I'm expecting the server where this will run on to require lots of RAM, and so i'm querying this data over time to be able to estimate RAM requirements when compared to the sizes of the lists it keeps inside.

So... Which one should I use and why?

+1  A: 

If you want to know how much the GC uses try:

GC.GetTotalMemory(true)

If you want to know what your process uses from Windows (VM Size column in TaskManager) try:

Process.GetCurrentProcess().PrivateMemorySize64

If you want to know what your process has in RAM (as opposed to in the pagefile) (Mem Usage column in TaskManager) try:

Process.GetCurrentProcess().WorkingSet64

See here for more explanation on the different sorts of memory.

Lars Truijens
A: 

Working set isn't a good property to use. From what I gather, it includes everything the process can touch, even libraries shared by several processes, so you're seeing double-counted bytes in that counter. Private memory is a much better counter to look at.

OwenP
+1  A: 

OK, I found through Google the same page that Lars mentioned, and I believe it's a great explanation for people that don't quite know how memory works (like me).

http://shsc.info/WindowsMemoryManagement

My short conclusion was:

  • Private Bytes = The Memory my process has requested to store data. Some of it may be paged to disk or not. This is the information I was looking for.

  • Virtual Bytes = The Private Bytes, plus the space shared with other processes for loaded DLLs, etc.

  • Working Set = The portion of ALL the memory of my process that has not been paged to disk. So the amount paged to disk should be (Virtual - Working Set).

Thanks all for your help!

Daniel Magliola
A: 

I'd suggest to also monitor how often pagefaults happen. A pagefault happens when you try to access some data that have been moved from physical memory to swap file and system has to read page from disk before you can access this data.

Michał Piaskowski
A: 

If you want to use the "Memory (Private Working Set)" as shown in Windows Vista task manager, which is the equivalent of Process Explorer "WS Private Bytes", here is the code. Probably best to throw this infinite loop in a thread/background task for real-time stats.

using System.Threading;
using System.Diagnostics;

//namespace...class...method

Process thisProc = Process.GetCurrentProcess();
PerformanceCounter PC = new PerformanceCounter();

PC.CategoryName = "Process";
PC.CounterName = "Working Set - Private";
PC.InstanceName = thisProc.ProcessName;

while (true)
{
 String privMemory = (PC.NextValue()/1000).ToString()+"KB (Private Bytes)";
 //Do something with string privMemory

 Thread.Sleep(1000);
}
Mike Regan