我的 Windows 应用程序真正使用了多少内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1166502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How much memory is my windows app really using?
提问by mmr
I have a long-running memory hog of an experimental program, and I'd like to know it's actual memory footprint. The Task Manager says (in windows7-64) that the app is consuming 800 mb of memory, but the total amount of memory allocated, also according to the task manager, is 3.7gb. The sum of all the allocated memory does not equal 3.7gb. How can I determine, on the fly, how much memory my application is actually consuming.
我有一个长期运行的实验程序的内存占用,我想知道它的实际内存占用量。任务管理器说(在 windows7-64 中)该应用程序消耗了 800 mb 的内存,但根据任务管理器的说法,分配的内存总量为 3.7gb。所有分配的内存总和不等于 3.7gb。如何即时确定我的应用程序实际消耗了多少内存。
Corollary: What memory is the task manager actually reporting? It doesn't seem to be all the memory that's allocated to the app itself.
推论:任务管理器实际报告的内存是多少?似乎并不是分配给应用程序本身的所有内存。
采纳答案by Kim Gr?sman
As I understand it, Task manager shows the Working Set;
据我了解,任务管理器显示工作集;
working set: The set of memory pages recently touched by the threads of a process. If free memory in the computer is above a threshold, pages are left in the working set of a process even if they are not being used. When free memory falls below a threshold, pages are trimmed from the working set.
工作集:进程线程最近触及的内存页集。如果计算机中的可用内存高于阈值,即使页面未被使用,页面也会留在进程的工作集中。当空闲内存低于阈值时,将从工作集中修剪页面。
via http://msdn.microsoft.com/en-us/library/cc432779(PROT.10).aspx
通过http://msdn.microsoft.com/en-us/library/cc432779(PROT.10).aspx
You can get Task Manager to show Virtual Memory as well.
您也可以让任务管理器显示虚拟内存。
I usually use perfmon (Start -> Run... -> perfmon) to track memory usage, using the Private Bytes counter. It reflects memory allocated by your normal allocators (new/HeapAlloc/malloc, etc).
我通常使用 perfmon (Start -> Run... -> perfmon) 来跟踪内存使用情况,使用 Private Bytes 计数器。它反映了您的普通分配器(new/HeapAlloc/malloc 等)分配的内存。
回答by IRBMe
Memory is a tricky thing to measure. An application might reserve lots of virtual memorybut not actually use much of it. Some of the memory might be shared; that is, a shared DLL might be loaded in to the address space of several applications but it is only loaded in to physical memory once.
记忆是一个很难衡量的东西。应用程序可能会保留大量虚拟内存,但实际上并未使用其中的大部分。一些内存可能是共享的;也就是说,一个共享的 DLL 可能会被加载到多个应用程序的地址空间中,但它只会被加载到物理内存中一次。
A good measure is the working set, which is the set of pages in its virtual address space that have been accessed recently. What the meaning of 'accessed recently' is depends on the operating system and its page replacement algorithm. In other words, it is the actual set of virtual pages that are mapped in to physical memory and are in use at the moment. This is what the task manager shows you.
一个很好的衡量标准是工作集,它是最近访问过的虚拟地址空间中的页面集。“最近访问过”的含义取决于操作系统及其页面替换算法。换句话说,它是映射到物理内存并正在使用的实际虚拟页面集。这是任务管理器向您显示的内容。
The virtual memory usage is the amount of virtual pages that have been reserved (note that not all of these will have actually been committed, that is, had physical backing store allocated for it. You can add this to the display in task manager by clicking View -> Select Columns.
虚拟内存使用量是已保留的虚拟页面的数量(请注意,并非所有这些页面实际上都已提交,即为其分配了物理后备存储。您可以通过单击将其添加到任务管理器中的显示中查看 -> 选择列。
The most important thing though: If you want to actually measure how much memory your program is using to see if you need to optimize some of it for space or choose better data structures or persist some things to disk, using the task manager is the wrong approach. You should almost certainly be using a profiler.
最重要的事情是:如果你想实际测量你的程序使用了多少内存来查看是否需要优化其中的一些空间或选择更好的数据结构或将一些内容持久化到磁盘,那么使用任务管理器是错误的方法。您几乎肯定应该使用分析器。
回答by IRBMe
The memory statistics displayed by task manager are not nearly all the statistics available, nor are particularly well presented. I would use the great free tool from Microsoft Sysinternals, VMMap, to analyse the memory used by the application further.
任务管理器显示的内存统计数据几乎不是所有可用的统计数据,也不是特别好呈现。我会使用 Microsoft Sysinternals 的免费工具VMMap来进一步分析应用程序使用的内存。
If it is a long running application, and the memory usage grows over time, it is going to be the heap that is growing. Parts of the heap may or may not be paged out to disk at any time, but you really need to optimize you heap usage. In this case you need to be profile your application. If it is a .Net application then I can recommend Redgate's ANTS profiler. It is very easy to use. If it's a native application, then the Intel vtune profiler is pretty powerful. You don't need the source code for the process you are profiling for either tool.
如果它是一个长时间运行的应用程序,并且内存使用量随着时间的推移而增长,那么它将是不断增长的堆。堆的一部分可能随时也可能不会被调出到磁盘,但您确实需要优化堆使用。在这种情况下,您需要配置您的应用程序。如果它是一个 .Net 应用程序,那么我可以推荐 Redgate 的 ANTS profiler。这是非常容易使用。如果它是本机应用程序,那么英特尔 vtune 分析器非常强大。您不需要为任一工具分析的过程的源代码。
Both applications have a free trial. Good luck.
这两个应用程序都有免费试用。祝你好运。
P.S. Sorry I didn't include more hyperlinks to the tools, but this is my first post, and stackoverflow limits first posts to one hyperlink :-(
PS抱歉,我没有包含更多指向工具的超链接,但这是我的第一篇文章,stackoverflow 将第一篇文章限制为一个超链接:-(
回答by JaredPar
That depends on what memory you are talking about. Unfortunately there are many different ways to measure memory. For instance ...
那要看你说的是什么内存了。不幸的是,有许多不同的方法来衡量记忆。例如 ...
- Physical Memory Allocated
- Virtual Memory Allocated
- Virtual Memory Reserved (but not committed)
- Private Bytes
- Shared Bytes
- 分配的物理内存
- 分配的虚拟内存
- 保留虚拟内存(但未提交)
- 私有字节
- 共享字节
Which metric are you interested in?
您对哪个指标感兴趣?
I think most people tend to be interested in the "Virtual Memory Allocated" category.
我认为大多数人倾向于对“分配的虚拟内存”类别感兴趣。