监视应用程序内存使用情况的正确方法是什么? [英] What is the right way to monitor application memory usage?

查看:336
本文介绍了监视应用程序内存使用情况的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于调试目的,我编写了这个小的静态方法:

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;
}

由于某些原因,VirtualMemorySize64返回的内存比Visual Studio的诊断工具"窗口以及任务管理器"所显示的要多得多.对于我现在正在运行的特定示例,这里是数字:

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:

  • 诊断工具:〜250 MB
  • 任务管理器:〜120 MB
  • VirtualMemorySize64:〜1100 MB

为什么会有如此大的差异?如何从应用程序内部正确跟踪内存使用情况?

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

推荐答案

VirtualMemorySize 测量您的进程使用的所有虚拟内存.其中包括计算机上所有其他进程共享的页面. .NET程序中包含操作系统,CLR,抖动和ngen-ed Framework程序集.

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.

所以:

如果要了解正在使用的内存量,请检索 VirtualMemorySize 工作集 私有字节 .

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.

  • 私有字节 与任务管理器误导性地称为"VM Size".
  • 任务管理器称为
  • 工作集 ,它是进程地址空间(专用字节"加上内存映射文件)的一部分.当前驻留在RAM中,并且可以在没有页面错误的情况下进行引用).
  • VirtualMemorySize 是进程的总虚拟地址空间,包括专用字节和内存映射的东西.
  • 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.

如果您将所有进程的 VirtualMemorySize 加起来,您可能会发现它加起来的内存比实际更多.这是因为可以在进程之间共享那些内存映射文件,EXE,DLL等.并且可以在多个进程的地址空间中一次访问RAM中的同一物理页面.

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.

这篇关于监视应用程序内存使用情况的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆