星期四, 十二月 07, 2006

free 输出说明及内存相关资料

使用 free 来输出内存使用情况:
# free
             total       used       free     shared    buffers     cached
Mem: 239052 232916 6136 0 11384 102684
-/+ buffers/cache: 118848 120204
Swap: 522072 140 521932
这里有必要对输出进行解释。

第一行(Mem)是针对 OS 操作系统而言的,对 OS 来说,后面的 buffers + cached 都属于被使用了。所以这里,操作系统已使用内存为 232916K,剩余可用内存为 6136K。

但对于 Applications 应用程序来说,buffers + cached 都属于可用内存,是操作系统将这些内存作为缓存以使程序运行更快(空间换时间)。所以第二行的情况是 used 118848K,free 120204K。

所以可以得到这样的运算关系:
1. total = Mem-used + Mem-free = buffers/cache-used + buffers/cache-free 
2. Mem-used = buffers/cache-used + Mem-buffers + Mem-cached

下面是 buffers 与 cached 的区别。

buffers 是指用来给块设备做的缓冲大小,他只记录文件系统的 metadata 以及 tracking in-flight pages. cached 是用来给文件做缓冲。那就是说:buffers 是用来存储,目录里面有什么内容,权限等等。而 cached 直接用来记忆我们打开的文件,如果你想知道他是不是真的生效,你可以试一下,先后执行两次命令 # man X ,你就可以明显的感觉到第二次的开打的速度快很多。

http://tech.ccidnet.com/art/3067/20060925/908747_1.html

The difference between buffers and cache

Buffers are allocated by various processes to use as input queues, etc. Most time, buffers are some processes' output, and they are file buffers. A simplistic explanation of buffers is that they allow processes to temporarily store input in memory until the process can deal with it.

Cache is typically frequently requested disk I/O. If multiple processes are accessing the same files, much of those files will be cached to improve performance (RAM being so much faster than hard drives), it's disk cache.

http://gentoo-wiki.com/FAQ_Linux_Memory_Management

下面的解释更专业一点,而且还有一段对于 top 的 VIRT, RES 和 SHR 的解释:
The difference among VIRT, RES, and SHR in top output

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.


另外还有一个 2.6 内核的 Swappiness 是比较有价值的信息:
Swappiness (2.6 kernels)

Since 2.6, there has been a way to tune how much Linux favors swapping out to disk compared to shrinking the caches when memory gets full.

When an application needs memory and all the RAM is fully occupied, the kernel has two ways to free some memory at its disposal: it can either reduce the disk cache in the RAM by eliminating the oldest data or it may swap some less used portions (pages) of programs out to the swap partition on disk. It is not easy to predict which method would be more efficient. The kernel makes a choice by roughly guessing the effectiveness of the two methods at a given instant, based on the recent history of activity.

Before the 2.6 kernels, the user had no possible means to influence the calculations and there could happen situations where the kernel often made the wrong choice, leading to thrashing and slow performance. The addition of swappiness in 2.6 changes this. Thanks, ghoti!

Swappiness takes a value between 0 and 100 to change the balance between swapping applications and freeing cache. At 100, the kernel will always prefer to find inactive pages and swap them out; in other cases, whether a swapout occurs depends on how much application memory is in use and how poorly the cache is doing at finding and releasing inactive items.

The default swappiness is 60. A value of 0 gives something close to the old behavior where applications that wanted memory could shrink the cache to a tiny fraction of RAM. For laptops which would prefer to let their disk spin down, a value of 20 or less is recommended.

As a sysctl, the swappiness can be set at runtime with either of the following commands:
sysctl -w vm.swappiness=30
echo 30 >/proc/sys/vm/swappiness

The default when Gentoo boots can also be set in /etc/sysctl.conf:
# Control how much the kernel should favor swapping out applications (0-100)
vm.swappiness = 30

Some patchsets (e.g. Con Kolivas' ck-sources patchset) allow the kernel to auto-tune the swappiness level as it sees fit; they may not keep a user-set value.

没有评论: