[ACCEPTED]-Process Memory Size - Different Counters-diagnostics

Accepted answer
Score: 17

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


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


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


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

Score: 9

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


My 11 short conclusion was:

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

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

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

Thanks 1 all for your help!

Score: 5

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

using System.Threading;
using System.Diagnostics;


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

Score: 3

To get the value that Task Manager gives, my 9 hat's off to Mike Regan's solution above. However, one 8 change: it is not: perfCounter.NextValue()/1000; but perfCounter.NextValue()/1024; (i.e. a real 7 kilobyte). This gives the exact value you 6 see in Task Manager.

Following is a full 5 solution for displaying the 'memory usage' (Task 4 manager's, as given) in a simple way in 3 your WPF or WinForms app (in this case, simply 2 in the title). Just call this method within 1 the new Window constructor:

    private void DisplayMemoryUsageInTitleAsync()
        origWindowTitle = this.Title; // set WinForms or WPF Window Title to field
        BackgroundWorker wrkr = new BackgroundWorker();
        wrkr.WorkerReportsProgress = true;

        wrkr.DoWork += (object sender, DoWorkEventArgs e) => {
            Process currProcess = Process.GetCurrentProcess();
            PerformanceCounter perfCntr = new PerformanceCounter();
            perfCntr.CategoryName = "Process";
            perfCntr.CounterName = "Working Set - Private";
            perfCntr.InstanceName = currProcess.ProcessName;

            while (true)
                int value = (int)perfCntr.NextValue() / 1024;
                string privateMemoryStr = value.ToString("n0") + "KB [Private Bytes]";
                wrkr.ReportProgress(0, privateMemoryStr);

        wrkr.ProgressChanged += (object sender, ProgressChangedEventArgs e) => {
            string val = e.UserState as string;
            if (!string.IsNullOrEmpty(val))
                this.Title = string.Format(@"{0}   ({1})", origWindowTitle, val);

Score: 2

Is this a fair description? I'd like to 15 share this with my team so please let me 14 know if it is incorrect (or incomplete):

There 13 are several ways in C# to ask how much memory 12 my process is using.

  • Allocated memory can be managed (by the CLR) or unmanaged.
  • Allocated memory can be virtual (stored on disk) or loaded (into RAM pages)
  • Allocated memory can be private (used only by the process) or shared (e.g. belonging to a DLL that other processes are referencing).

Given the above, here 11 are some ways to measure memory usage in 10 C#:

1) Process.VirtualMemorySize64(): returns 9 all the memory used by a process - managed 8 or unmanaged, virtual or loaded, private 7 or shared.

2) Process.PrivateMemorySize64(): returns 6 all the private memory used by a process 5 - managed or unmanaged, virtual or loaded.

3) Process.WorkingSet64(): returns 4 all the private, loaded memory used by a 3 process - managed or unmanaged

4) GC.GetTotalMemory(): returns 2 the amount of managed memory being watched 1 by the garbage collector.

Score: 0

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

Score: 0

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

More Related questions