Working set和Private bytes区别

在Performance monitor中可以通过private bytes和Virtual bytes来衡量程序的内存使用. 在task manager中, 也有Memory Usage和VM Size两项. 但是仔细比较后会发现Memory Usage并不是对应private bytes, VM Size也不是对应Virtual Bytes.

 

其实, task manager中的Memory Usage对应的是working set, VM Size对应的是private bytes. 因此如果使用task manager观察内存使用, 应该注意到这个差别.

 

Working Set和Private Bytes

==============

一个有趣的问题是, working set指目前程序所消耗的物理内存, private bytes值commit的内存, 那么为什么有些进程的working set比private bytes还大? 要回答这个问题, 需要仔细看看两者的定义:

  • "Working Set refers to the numbers of pages of virtual memory committed to a given process, both shared and private."
  • "Private Bytes is the current size, in bytes, of memory that this process has allocated that cannot be shared with other processes."

所以, Working Set包含了可能被其他程序共享的内存, 而Private Bytes只包括被当前进程使用的内存.

 

DLL是一个典型的可能被其他程序共享的资源. DLL的加载使用文件映像, 因此包含DLL的物理内存可以被同时映像到多个进程上. 所以在进程中加载DLL的内存只能算到working set上, 而不能被算到private bytes上.

 

2010年4月15日更新:

在解决内存问题的时候Shared的部分一般可以不用考虑.

一个进程使用内存的时候, 它占用的内存会被分为两部分, 一部分是working set, 另一部分是private byte减去working set. 其中working set是贮存在物理内存中的, 而另一部分是paging file, 存在磁盘上.

 

一般来说把所有进程的working set加起来会比你机器上所拥有的物理内存要大, 这是因为有Shared的资源(比如DLL)的缘故.

 

关于Working Set, Virtual Bytes, Private Byte的概念

Working Set- Working Set is the current size, in bytes, of the Working Set of this process. The Working Set is the set of memory pages touched recently by the threads in the 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 in use. When free memory falls below a threshold, pages are trimmed from Working Sets. If they are needed they will then be soft-faulted back into the Working Set before leaving main memory.

 

Virtual Bytes- Virtual Bytes is the current size, in bytes, of the virtual address space the process is using. Use of virtual address space does not necessarily imply corresponding use of either disk or main memory pages. Virtual space is finite, and the process can limit its ability to load libraries.

 

Private Bytes- Private Bytes is the current size, in bytes, of memory that this process has allocated that cannot be shared with other processes.

 

=======================================

 

Private Bytes refer to the amount of physical memory (RAM) that the process executable has asked for - not necessarily the amount it is actually using. Private bytes are "private" because they (usually) exclude memory-mapped files (i.e. shared DLLs). But - here's the catch - they don't necessarily exclude memory allocated by those files. So if your executable depends on any libraries - and almost every executable does - you cannot always tell whether a change in private bytes was due to the executable itself or some library it links to. Also note that "physical memory" is not a perfect description - what it really means is bytes that can be addressed without a page fault.

 

Working Set refers to the total physical address space used by the process. This includes memory-mapped files and quite possibly several other things; a 32-bit process can access 4 GB of address space and not all of that address space is actually memory. This is the same value that gets reported in Task Manager's "Mem Usage" and has been the source of endless amounts of confusion in recent years. Similarly to private bytes, this is "physical" in the sense that it can be addressed without a page fault.

 

Virtual Bytes are the total virtual address space occupied by the entire process, including memory-mapped files such as shared DLLs. This is like the working set except it includes data that has already been paged out and is sitting in a pagefile somewhere. The total virtual bytes used by every process on a system under heavy load will add up to significantly more memory than the machine actually has.

你可能感兴趣的:(Working set和Private bytes区别)