relatively_random
relatively_random

Reputation: 5146

What is the right way to monitor application memory usage?

For debugging purposes, I have written this little static method:

public static long CheckMemory(long maxMemorySizeBytes)
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();

    var usedMemoryBytes = Process.GetCurrentProcess().VirtualMemorySize64;
    if (usedMemoryBytes > maxMemorySizeBytes)
        Debugger.Break();

    return usedMemoryBytes;
}

For some reason, VirtualMemorySize64 keeps returning lots more memory than what Visual Studio Diagnostic Tools window is showing, as well as what the Task Manager is showing. For the specific example I'm running now, here are the numbers:

Why are there such large differences and how do I properly track memory usage from within the app itself?

Upvotes: 9

Views: 6049

Answers (1)

SᴇM
SᴇM

Reputation: 7213

VirtualMemorySize measures all of the virtual memory that your process uses. Which includes the pages shared by all other processes on your machine. Which in a .NET program includes the operating system, CLR, jitter and the ngen-ed Framework assemblies.

Diagnostic tools - Displays a live graph of your application’s Private Bytes metric. Private Bytes is a measure of the total amount of memory that a process has allocated, not including memory shared with other processes.

In Task Manager, by default, you see the "private working set" memory, which is the amount of memory used by a process that cannot be shared among other processes.

So:

If you want to get an idea of how much memory you're using, retrieve the VirtualMemorySize, Working Set and Private Bytes for a process.

  • Private Byte is the same thing the Task Manager misleadingly refers to as "VM Size".
  • Working Set is what the Task Manager calls "Mem Usage", which is the portion of the processes' address space ("Private Bytes" plus memory mapped files) that is currently resident in RAM and can be referenced without a page fault).
  • VirtualMemorySize is the total virtual address space of the process, which includes both private bytes and memory-mapped stuff.

If you add up all of the processes' VirtualMemorySize, you'll likely find it adds up to way more memory than you actually have. That's because those memory-mapped files, EXEs, DLLs, etc. can be shared among processes; and the same physical page in RAM can be accessed in more than one's process's address space at once.

Upvotes: 12

Related Questions