What is the right way to monitor application memory usage?

后端 未结 1 1347
礼貌的吻别
礼貌的吻别 2021-02-06 07:26

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

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


        
1条回答
  •  悲哀的现实
    2021-02-06 07:51

    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.

    0 讨论(0)
提交回复
热议问题