Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Diagnostic Tools: ~250 MB
  • Task Manager: ~120 MB
  • VirtualMemorySize64: ~1100 MB

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

like image 806
relatively_random Avatar asked Sep 11 '17 10:09

relatively_random


People also ask

How do I monitor the memory usage of an application?

Search for Resource Monitor and click the top result to open the app. Click the Memory tab. Click the Private (KB) header to sort the applications by memory usage. (The information is presented in kilobytes.)

What is a CPU and memory usage monitor?

What is a CPU and memory usage monitor? A full-stack CPU and memory usage monitor serves to collect saturation metrics from your on-premises server. Saturation is one of the four golden signals of resource monitoring, and it allows you to understand when CPU utilization and memory are at or near capacity.

How do I check the amount of RAM an app is using?

To check the amount of RAM that apps are using, follow these steps: Open Start. Search for Resource Monitor and click the top result to open the app. Click the Memory tab. Click the Private (KB) header to sort the applications by memory usage. (The information is presented in kilobytes.)

How do I know which apps are using the most memory?

To determine which apps are using the most memory, use these steps: Open Start. Search for Task Manager and click the top result to open the app. Quick tip: You can also right-click the taskbar and select the Task Manager option, or right-click the Start button and select the Task Manager option.


1 Answers

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.

like image 183
SᴇM Avatar answered Sep 28 '22 13:09

SᴇM