简体   繁体   English

监控应用程序内存使用情况的正确方法是什么?

[英]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. 出于某种原因, VirtualMemorySize64不断返回比Visual Studio Diagnostic Tools窗口显示的内存更多的内存,以及任务管理器显示的内容。 For the specific example I'm running now, here are the numbers: 对于我现在正在运行的具体示例,以下是数字:

  • Diagnostic Tools: ~250 MB 诊断工具:~250 MB
  • Task Manager: ~120 MB 任务管理器:~120 MB
  • VirtualMemorySize64: ~1100 MB VirtualMemorySize64:~1100 MB

Why are there such large differences and how do I properly track memory usage from within the app itself? 为什么会出现如此大的差异,如何从应用程序本身内正确跟踪内存使用情况?

VirtualMemorySize measures all of the virtual memory that your process uses. VirtualMemorySize测量进程使用的所有虚拟内存。 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. 在.NET程序中包含操作系统,CLR,抖动和ngen-ed框架程序集。

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. Private Bytes是进程分配的内存总量的度量,不包括与其他进程共享的内存。

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. 如果您想了解您正在使用多少内存,请检索进程的VirtualMemorySizeWorking SetPrivate Bytes

  • Private Byte is the same thing the Task Manager misleadingly refers to as "VM Size". 私有字节与任务管理器误导性地称为“VM大小”相同。
  • 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). 工作集是任务管理器所谓的“内存使用”,它是进程的地址空间(“专用字节”加上内存映射文件)的一部分,当前驻留在RAM中并且可以在没有页面错误的情况下被引用。
  • VirtualMemorySize is the total virtual address space of the process, which includes both private bytes and memory-mapped stuff. VirtualMemorySize是进程的总虚拟地址空间,包括专用字节和内存映射内容。

If you add up all of the processes' VirtualMemorySize , you'll likely find it adds up to way more memory than you actually have. 如果你将所有进程的VirtualMemorySize相加,你可能会发现它增加了比你实际拥有的内存更多的内存。 That's because those memory-mapped files, EXEs, DLLs, etc. can be shared among processes; 那是因为那些内存映射文件,EXE,DLL等可以在进程之间共享; and the same physical page in RAM can be accessed in more than one's process's address space at once. RAM中的相同物理页面可以同时在多个进程的地址空间中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM