Linux:内存(Command,free)
在Linux里面可以通过“free”查看内存当前的使用量和空闲量:
1 2 3 4 5 6 7 8 9 |
[root@rhel510 ~]# free -m -l -t total used free shared buffers cached Mem: 3944 3880 63 0 41 3462 Low: 3944 3880 63 High: 0 0 0 -/+ buffers/cache: 376 3567 Swap: 5043 0 5043 Total: 8988 3880 5107 [root@rhel510 ~]# |
在free命令的结果集中:
total,内存总容量(物理内存);
used,已经使用的内存数;
free,空闲的内存数;
它们三个的关系是:total = used + free;
shared,被多个进程共享的内存;
buffers,被OS Buffer的内存;
cached,被OS Cache的内存;
buffer和cache是有区别的:
被Buffer的内存的数据:还没有,但需要被写到了磁盘;
被cached的内存的数据:从磁盘上读取到的,并驻留在内存中,以便于后续使用;
外文的描述可能比较容易理解,是这样的:
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还是cache都是为了提高IO性能而被设计出来的,它们被OS所管理。
而Linux为了提高IO Read的性能,总是会倾向于多cache一些数据的。表现出来的现象是:cached的值比较大。
释放被OS cache持有的数据:
命令:echo 3 > /proc/sys/vm/drop_caches
Shell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[root@rhel510 ~]# free -m -l -t total used free shared buffers cached Mem: 3944 3882 62 0 43 3462 Low: 3944 3882 62 High: 0 0 0 -/+ buffers/cache: 376 3568 Swap: 5043 0 5043 Total: 8988 3882 5106 [root@rhel510 ~]# [root@rhel510 ~]# cat /proc/sys/vm/drop_caches 0 [root@rhel510 ~]# [root@rhel510 ~]# echo 3 > /proc/sys/vm/drop_caches [root@rhel510 ~]# cat /proc/sys/vm/drop_caches 3 [root@rhel510 ~]# [root@rhel510 ~]# free -m -l -t total used free shared buffers cached Mem: 3944 292 3651 0 0 56 Low: 3944 292 3651 High: 0 0 0 -/+ buffers/cache: 235 3708 Swap: 5043 0 5043 Total: 8988 293 8695 [root@rhel510 ~]# |
在free命令的结果集中,还需要关注的是:“-/+ buffers/cache”。
它所对应的两个值(同一行)分别表示:
– buffers/cache,一个应用程序认为系统用掉的内存;(计算方法:“- buffers/cache” = used – buffers – cached)
+ buffers/cache,一个应用程序认为系统还有的内存;(计算方法:“- buffers/cache” = free + buffers + cached)
在操作系统中,被cache与buffer占用的内存因为可以被快速回收,所以会出现(应用认为系统可用的内存)对应的值比(应用认为系统用掉的内存)的值大很多的现象。
——————————
关于“free”命令本身:
来源(RPM包):
1 2 3 4 5 6 |
[root@rhel510 ~]# whereis free free: /usr/bin/free /usr/share/man/man3p/free.3p.gz /usr/share/man/man1/free.1.gz /usr/share/man/man3/free.3.gz [root@rhel510 ~]# [root@rhel510 ~]# rpm -qf /usr/bin/free procps-3.2.7-26.el5 [root@rhel510 ~]# |
可以看到,free是由procps软件包提供的。
命令“free”的数据来源是:/proc/meminfo
1 2 3 4 5 |
[root@rhel510 ~]# file /usr/bin/free /usr/bin/free: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped [root@rhel510 ~]# strings /usr/bin/free | grep --color meminfo meminfo [root@rhel510 ~]# |
关于/proc/meminfo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[root@rhel510 ~]# cat /proc/meminfo MemTotal: 4039376 kB MemFree: 3729620 kB Buffers: 3476 kB Cached: 67160 kB SwapCached: 0 kB Active: 203976 kB Inactive: 19684 kB HighTotal: 0 kB HighFree: 0 kB LowTotal: 4039376 kB LowFree: 3729620 kB SwapTotal: 5164888 kB SwapFree: 5164804 kB Dirty: 56 kB Writeback: 0 kB AnonPages: 153032 kB Mapped: 57896 kB Slab: 29264 kB PageTables: 21000 kB NFS_Unstable: 0 kB Bounce: 0 kB CommitLimit: 7184576 kB Committed_AS: 444604 kB VmallocTotal: 34359738367 kB VmallocUsed: 267204 kB VmallocChunk: 34359471159 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 Hugepagesize: 2048 kB [root@rhel510 ~]# |
———————————————
Done。