问题描述
我一直在跟踪应用程序中的一些内存泄漏.确实很痛苦,但是我终于把所有东西都收紧了.但是,有一点Windows内存管理使我感到困惑.这是该应用随时间推移的内存使用情况的打印输出...
I've been tracking down a few memory leaks in my application. It's been a real pain, but I've finally tightened everything up. However, there's one bit of Windows memory management that is confusing me. Here is a printout of the app's memory usage over time...
Time PrivateMemorySize64 WorkingSet64
20:00:36 47480, 50144
20:01:06 47480, 50144
20:01:36 47480, 50144
20:02:06 47480, 149540
20:02:36 47480, 149540
20:03:06 47480, 149540
工作集在30秒的时间内从49 MB跃升至146.由于该应用程序基本上什么都不做,因此发生了一整夜.
The working set jumped from 49 MB to 146 over a span of 30 seconds. This happened overnight as the application was basically doing nothing.
工作集(任务管理器向我显示的内容)似乎能够受到其他应用程序(例如调试器)的影响(正如我在寻找内存泄漏时所了解的那样).在阅读了有关什么是工作集的文档之后,我仍然没有很好的理解.
The working set (which is what task manager shows me) seems to be able to be influenced by other applications such as debuggers (as I learned while looking for memory leaks). After reading the documentation on what Working Set is, I still don't have a good understanding.
感谢您的帮助.
更新:由于响应者提供了一些链接以及其他一些搜索功能,我对一个单独的流程如何导致我的流程工作集增长有了更好的了解.很高兴知道工作集中的峰值并不一定表示您的应用程序正在泄漏...不依赖任务管理器进行内存评估的进一步原因:)
Update: Thanks to some links from responders as well as some additional searching, I have a better understanding on how a separate process can cause my process' working set to grow. Good to know that a spike in the working set is not necessarily an indication that your app is leaking... Further reason to not rely on Task Manager for memory evaluation :)
有用的链接:
推荐答案
简单地说,工作集是您的进程当前拥有但未交换(即在RAM中)的内存页面的集合.但是,这有点不准确.现实要复杂得多.
Simply said, the working set is the collection of memory pages currently owned by your process and not swapped out (i.e. in RAM). That is somewhat inaccurate, however. Reality is a lot more complicated.
Windows为每个进程维护最小工作集大小和最大工作集大小.最小的工作集很容易,这是Windows授予每个进程的权限(只要它可以,受物理限制).
Windows maintains a minimum working set size and a maximum working set size for every process. The minimum working set is easy, it is what Windows will grant to every process (as long as it can, by physical limits).
最大工作集更令人怀疑.如果您的程序使用的内存超出其配额所允许的内存,则Windows将删除某些页面.但是,尽管它们不再出现在您的工作集中,但不一定已消失".
The maximum working set is more dubious. If your program uses more memory than will fit in its quota, Windows will drop some pages. However, while they are no longer in your working set, these pages are not necessarily "gone".
相反,这些页面将从您的工作集中删除,并移至可用页面池中.因此,如果某些其他程序需要更多的内存并且没有剩余已清除的页面,则将清除您的页面,并将其分配给其他进程.当您访问它们时,如果您仍然超出最大工作集大小,则需要再次从交换文件中获取它们,可能会清除其他页面.
Rather, those pages are removed from your working set and moved to the pool of available pages. As a consequence, if some other program needs more memory and no cleared pages are left over, your pages will be cleared, and assigned to a different process. When you access them, they will need to be fetched from the swapfile again, possibly purging other pages, if you are still above the maximum working set size.
但是,如果在此期间没有人要求更多的内存(或者无论是否可以通过未使用的页面来满足所有需求),那么访问这些页面中的一个只会使其神奇地重新出现"并踢出另一个页面代替它.
However, if nobody asked for more memory in the mean time (or if all demands could be satisfied by pages that were unused anyway), then accessing one of those pages will simply make it "magically reappear" and kick out another page in its stead.
因此,您的进程在RAM中的页数可能比其工作集中的实际页数多,但它不是正式"拥有它们.
Thus, your process can have more pages in RAM than are actually in its working set, but it does not "officially" own them.
这篇关于帮助了解Windows内存-“工作集"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!