free

[root@molidao ~]# man free
FREE(1)                                                Linux User’s Manual                                                FREE(1)

NAME
free – Display amount of free and used memory in the system

SYNOPSIS
free [-b | -k | -m] [-o] [-s delay ] [-t] [-V]

DESCRIPTION
free displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the
kernel.  The shared memory column should be ignored; it is obsolete.

Options
The -b switch displays the amount of memory in bytes; the -k switch (set by default) displays  it  in  kilobytes;  the  -m
switch displays it in megabytes.

The -t switch displays a line containing the totals.

The  -o switch disables the display of a “buffer adjusted” line.  If the -o option is not specified, free subtracts buffer
memory from the used memory and adds it to the free memory reported.

The -s switch activates continuous polling delay seconds apart. You may actually specify any  floating  point  number  for
delay, usleep(3) is used for microsecond resolution delay times.

The -V displays version information.

FILES
/proc/meminfo– memory information

SEE ALSO
ps(1), slabtop(1), vmstat(8), top(1)

[root@m ~]# free
total       used       free     shared    buffers     cached
Mem:       4144672    4064284      80388          0      36688    3136092
-/+ buffers/cache:     891504    3253168
Swap:      8193140        192    8192948

total 内存总数: 4144672
used 已经使用的内存数: 4064284
free 空闲的内存数: 80388
shared 当前已经废弃不用,总是0
buffers Buffer Cache内存数: 36688
cached Page Cache内存数: 3136092

关系:

total = Mem.used + Mem.free

buffers = Mem.used + Mem.buffers + Mem.cached

cache = Mem.free + Mem.buffers + Mem.cached

total = used + free

第2行:
-/+ buffers/cache的意思相当于:
-buffers/cache 的内存数:891504 (等于第1行的 used – buffers – cached),即:891504 = 4064284 – 36688 – 3136092
+buffers/cache 的内存数: 3253168(等于第1行的 free + buffers + cached),即:3253168 = 80388 + 36688 + 3136092

可见-buffers/cache反映的是被程序实实在在吃掉的内存,而+buffers/cache反映的是可以挪用的内存总数。

第三行单独针对交换分区, 就不用再说了.

为了提高磁盘存取效率, Linux做了一些精心的设计, 除了对dentry进行缓存(用于VFS,加速文件路径名到inode的转换), 还采取了两种主要Cache方式:Buffer Cache和Page Cache。前者针对磁盘块的读写,后者针对文件inode的读写。这些Cache有效缩短了 I/O系统调用(比如read,write,getdents)的时间。

如果感兴趣可以进一步参考文件/proc/meminfo,free命令就是根据它的信息生成的。free命令的源码可从procps-xxx-.src.rpm获取,xxx为版本号,比如procps-3.2.3-5.3.src.rpm。

参考内核代码:

arch/i386/mm/init.c
fs/proc/proc_misc.c
include/linux/swap.h
mm/filemap.c
fs/buffer.c

另外还可以参考O’REILLY的书《Understanding the LINUX KERNEL》。

发表评论