简体   繁体   English

C#内存使用率

[英]C# memory usage

How I can get the actual memory used in my C# application? 如何获得C#应用程序中使用的实际内存?

  • Task Manager shows different metrics. 任务管理器显示不同的指标。
  • Process Explorer shows increased usage of private bytes. Process Explorer显示专用字节的使用增加。
  • Performance counter (perfmon.msc) showed different metrics 性能计数器(perfmon.msc)显示了不同的指标
  • when I used .NET memory profiler, it showed most of the memory is garbage collected and only few Live bytes. 当我使用.NET内存探查器时,它显示大部分内存是垃圾回收的,并且只有很少的活动字节。

I do not know which to believe. 我不知道该相信哪个。

Memory usage is somewhat more complicated than displaying a single number or two. 内存使用要比显示一个或两个数字复杂得多。 I suggest you take a look at Mark Russinovich's excellent post on the different kinds of counters in Windows. 我建议您看看Mark Russinovich在Windows中各种计数器上的出色文章

.NET only complicates matters further. .NET只会使事情进一步复杂化。 A .NET process is just another Windows process, so obviously it will have all the regular metrics, but in addition to that the CLR acts as a memory manager for the managed application. .NET进程只是Windows的另一个进程,因此显然它将具有所有常规指标,但除了CLR之外,它还充当托管应用程序的内存管理器。 So depending on the point of view these numbers will vary. 因此,根据观点,这些数字会有所不同。

The CLR effectively allocates and frees virtual memory in big chunks on behalf of the .NET application and then hands out bits of memory to the application as needed. CLR代表.NET应用程序有效地大块分配和释放虚拟内存,然后根据需要将一些内存分配给应用程序。 So while your application may use very little memory at a given point in time this memory may or may not have been released to the OS. 因此,尽管您的应用程序在给定的时间点可能只使用了很少的内存,但该内存可能已经释放或可能尚未释放到操作系统。

On top of that the CLR itself uses memory to load IL, compile IL to native code, store all the type information and so forth. 最重要的是,CLR本身使用内存来加载IL,将IL编译为本地代码,存储所有类型信息等等。 All of this adds to the memory footprint of the process. 所有这些都增加了进程的内存占用。

If you want to know how much memory your managed application uses for data, the Bytes in all heaps counter is useful. 如果您想知道托管应用程序用于数据的内存量,则“所有堆中的字节数”计数器将非常有用。 Private bytes may be used as a somewhat rough estimate for the application's memory usage on the process level. 专用字节可以用作进程级别上应用程序内存使用情况的某种粗略估计。

You may also want to check out these related questions: 您可能还需要检查以下相关问题:

Reducing memory usage of .NET applications? 减少.NET应用程序的内存使用量?

How to detect where a Memory Leak is? 如何检测内存泄漏在哪里?

If you are using VS 2010 you can use Visual Studio 2010 Profiler. 如果使用的是VS 2010,则可以使用Visual Studio 2010 Profiler。 This tool can create very informative reports for you. 该工具可以为您创建非常有用的报告。

If you want to know approximately how many bytes are allocated on the GC heap (ignoring memory used by the runtime, the JIT compiler, etc.), you can call GC.GetTotalMemory . 如果您想知道大约在GC堆上分配了多少字节(忽略运行时,JIT编译器等使用的内存),则可以调用GC.GetTotalMemory We've used this when tracking down memory leaks. 我们在跟踪内存泄漏时使用了它。

Download VADump (If you do not have it yet) 下载VADump (如果尚未安装)

Usage: VADUMP.EXE -sop [PID] 用法: VADUMP.EXE -sop [PID]

Well, what is "actual memory used in my C# application" ? 那么,什么是“我的C#应用​​程序中使用的实际内存”?

Thanks to Virtual memory and (several) Memory management layers in Windows and the CLR, this is a rather complicated question. 得益于Windows和CLR中的虚拟内存和(几个)内存管理层,这是一个相当复杂的问题。

From the sources you mention the CLR profiler will give you the most detailed breakdown, I would call that the most accurate. 从您提到的来源来看,CLR分析器将为您提供最详细的细分,我认为这是最准确的。

But there is no 'single number' answer, the question whether Application A use more or less memory than B can be impossible to answer. 但是没有“单数”答案,应用A是否使用比B更多或更少的内存这一问题可能无法回答。

So what do you actually want to know? 那么您实际上想知道什么? Do you have a concrete performance problem to solve? 您有具体的性能问题要解决吗?

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

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