free

Table of Contents

1. free

free 命令用于显示系统中的内存使用情况。

指定选项 -m/-g 后,会以 MB/GB 为单位进行显示内存使用情况,如:

$ free -m
             total       used       free     shared    buffers     cached
Mem:          1906       1072        834          0        109        502
-/+ buffers/cache:        461       1445
Swap:         1952          0       1952

除表头外,第 1 行中数字的说明如下:
total:表示物理内存总量,如例子中 1906 MB。
used:表示已经分配的内存(包含未使用的 buffers 和 cache),如例子中 1072 MB。
free:未被分配的内存,如例子中 834 MB。
shared:共享内存,一般系统不会用到,这里也不讨论。
buffers: 系统分配但未被使用的 buffers 数量, 如例子中 109 MB。
cached: 系统分配但未被使用的 cache 数量, 如例子中 502 MB。

除表头外,第 2 行中数字的说明如下(一般看这行就够了):
used:也就是第一行中的 used-buffers-cached,它表示实际已使用的内存。上面例子,有 461=1072-109-502。
free:未被使用的 buffers 与 cache 和未被分配的内存之和,这就是系统当前的可用内存。上面例子,有 1445=834+109+502。

参考:http://www.linuxso.com/command/free.html

1.1. buffer 与 cache 的区别

A buffer is something that has yet to be "written" to disk.
A cache is something that has been "read" from the disk and stored for later use.

Buffer is a region of memory used to temporarily hold data while it is being moved from one place to another.
Cache is a temporary storage area where frequently accessed data can be stored for rapid access.

1.2. 释放 cache

我们可以通过修改 /proc/sys/vm/drop_caches 来释放 cache,其操作如下:

$ sync
$ sudo su                            # 切换到root用户
$ echo 1 > /proc/sys/vm/drop_caches  # Clear page cache only.
$ echo 2 > /proc/sys/vm/drop_caches  # Clear dentries and inodes.
$ echo 3 > /proc/sys/vm/drop_caches  # Clear page cache, dentries and inodes.

pagecache/dentries/inodes 的说明如下:
The page cache could contain any memory mappings to blocks on disk.
An inode in memory caching is a data structure that represents a file.
A dentries in memory caching is a data structure that represents a directory.

参考:https://stackoverflow.com/questions/29870068/what-are-pagecache-dentries-inodes

Author: cig01

Created: <2016-06-01 Wed>

Last updated: <2020-05-09 Sat>

Creator: Emacs 27.1 (Org mode 9.4)