关于Linux的VIRT、RES、SHR内存

Linux的top命令会展示VIRT(虚拟内存)、RES(常驻内存)、SHR(共享内存)的使用量。man top里的说明也不怎么准确,例如这个公式: VIRT = SWAP + RES 肯定不对。如果进程没有使用交换内存,那么RES就会等于VIRT?这几乎不可能。在top里按f再按p,会打印SWAP(交换内存),这个交换内存就是按照上述公式算出来的,明显错误。

看到另外一篇blog的描述,引用如下:

VIRT stands for the virtual size of a process, which is the sum of memory it is actually using, memory it has mapped into itself (for instance the video card’s RAM for the X server), files on disk that have been mapped into it (most notably shared libraries), and memory shared with other processes. VIRT represents how much memory the program is able to access at the present moment.

RES stands for the resident size, which is an accurate representation of how much actual physical memory a process is consuming. (This also corresponds directly to the %MEM column.) This will virtually always be less than the VIRT size, since most programs depend on the C library.

SHR indicates how much of the VIRT size is actually sharable (memory or libraries). In the case of libraries, it does not necessarily mean that the entire library is resident. For example, if a program only uses a few functions in a library, the whole library is mapped and will be counted in VIRT and SHR, but only the parts of the library file containing the functions being used will actually be loaded in and be counted under RES.

我觉得上述描述比较靠谱,翻译过来大概如下:

  • 虚拟内存就是进程分配的所有内存,包括进程自身分配的、从磁盘加载共享库的、与其他进程共享的内存。另外还应包括交换内存。
  • 常驻内存是进程实际在使用的物理内存,不包括交换内存。
  • 共享内存也算在虚拟内存里,但是并非所有共享内存都是常驻内存。例如某程序使用了共享库里的一些函数,那么整个库会映射进来算在虚拟内存和共享内存里。但仅仅这个库文件里,包含了那些运行函数的部分,会被加载进来算作常驻内存。
此条目发表在Common分类目录,贴了, 标签。将固定链接加入收藏夹。