概要
free命令(free and used memory in the system)报告系统中已经使用和未使用的内存容量信息。包括实体内存,虚拟的交换文件内存,共享内存区段,以及系统核心使用的缓冲区等。这些信息是通过解析/ proc / meminfo收集的。
安装方式
#free属于procps软件包。
#Ubuntu
apt-get install procps
#CentOS
yum install procps-ng
procps软件包包含监视和终止系统进程的程序。它通过/proc目录搜集进程的信息.
- 包含的程序: free, kill, pgrep, pkill, ps, skill, snice, sysctl, tload, top, uptime, vmstat, w 和 watch
- 需要下载的库文件: libproc.so
free常用命令格式:
free [-bkmotV][-s <间隔秒数>]
参数说明:
-b 以Byte为单位显示内存使用情况。
-k 以KB为单位显示内存使用情况。
-m 以MB为单位显示内存使用情况。
-g 以GB为单位显示内存使用情况。
-h 以合适的单位显示内存使用情况,最大为三位数,自动计算对应的单位值。
-o 不显示缓冲区调节列。
-s<间隔秒数> 持续观察内存使用状况。
-t 显示内存总和列。
-V 显示版本信息。
注意:free命令的选项区分大小写
命令常用形式
$ free -m //以MB为单位显示内存使用情况。
total used free shared buff/cache available
Mem: 96239 4339 19430 4048 72469 87365
Swap: 61033 1046 59987
$ free -s 1 //每1s 执行一次命令
total used free shared buff/cache available
Mem: 98548932 3459704 20778812 4202780 74310416 90389332
Swap: 62498812 1071912 61426900
total used free shared buff/cache available
Mem: 98548932 3460256 20778260 4202780 74310416 90388780
Swap: 62498812 1071912 61426900
$ free -c 2 //执行两次命令
total used free shared buff/cache available
Mem: 98548932 2303972 91711204 4194556 4533756 91608664
Swap: 62498812 1071804 61427008
total used free shared buff/cache available
Mem: 98548932 2305592 91710960 4194556 4532380 91608180
Swap: 62498812 1071804 61427008
$ free -t //以总和的形式显示内存的使用信息
total used free shared buff/cache available
Mem: 98548932 4482860 19809156 4178196 74256916 89390700
Swap: 62498812 1071928 61426884
Total: 161047744 5554788 81236040
$ free -th
total used free shared buff/cache available
Mem: 93G 4.3G 18G 4.0G 70G 85G
Swap: 59G 1.0G 58G
Total: 153G 5.3G 77G
- 如果我们需要每列的总计信息,我们可以在 free 命令后面跟上 -t 选项。这会在字底部额外加入一行显示。
- 注意:free命令使用
-t显示内存使用情况时,存在如下关系:used= total - free - buffers - cache
输出说明:
used:已经使用的内存数free:完全没有被使用的,空闲的内存数shared:是被程序之间可以(已经被)共享使用的内存总额。(当前已经废弃不用;不算在总数里)buffers:是指用来给块设备做的缓冲大小,它只记录文件系统的metadata(目录,权限,属性等)以及 tracking in-flight pagescached:是用来给文件做缓冲,cached Page。total:内存总数;swap: 这部分是指交换分区,当可用内存少于额定值的时候,就会开会进行交换。可以使用cat /proc/meminfo看额定值。
(-/+ buffers/cache) 解释(如果有):
- (-buffers/cache) used内存数:第一部分Mem行中的 used – buffers – cached
- (+buffers/cache) free内存数: 第一部分Mem行中的 free + buffers + cached
总结:-buffers/cache反映的是被程序实实在在吃掉的内存,而+buffers/cache反映的是可以挪用的内存总数。
观测分析
- 通过free命令查看空闲内存时,会发现free的值很小。主要是因为,在Linux系统中有这么一种思想,内存不用白不用,因此它尽可能的cache和buffer一些数据,以方便下次使用。但实际上这些内存也是可以立刻拿来使用的。
- 内存够不够用重点是要看(-/+ buffers/cache)的free和used为主。
Linux为了提高磁盘和内存存取效率,做了很多精心的设计,除了对dentry进行缓存(用于VFS,加速文件路径名到inode的转换),还采取了两种主要Cache方式:Buffer Cache和Page Cache。前者针对磁盘块的读写,后者针对文件inode的读写。这些Cache能有效缩短了 I/O系统调用(比如read,write,getdents)的时间。
- Page cache主要用来作为文件系统上的文件数据的缓存来用,尤其是针对当进程对文件有read/write操作的时候。
- Buffer cache则主要是设计用来在系统对块设备进行读写的时候,对块进行数据缓存的系统来使用。
相关资料:
Cache 和 Buffer 都是缓存,主要区别是什么?
linux Page cache和buffer cache正解
背景资料
Linux 释放内存空间
Linux 服务器运行一段时间后,由于其内存管理机制,会将暂时不用的内存转为 buff/cache,这样在程序使用到这一部分数据时,能够很快的取出,从而提高系统的运行效率,所以这也正是 Linux 内存管理中非常出色的一点,所以乍一看内存剩余的非常少,但是在程序真正需要内存空间时,Linux 会将缓存让出给程序使用,这样达到对内存的最充分利用,所以真正剩余的内存是free+buff/cache。
但是有些时候大量的缓存占据空间,这时候应用程序回去使用 swap 交换空间,从而使系统变慢,这时候需要手动去释放内存,释放内存的时候,首先执行命令 sync 将所有正在内存中的缓冲区写到磁盘中,其中包括已经修改的文件 inode、已延迟的块 I/O 以及读写映射文件,从而确保文件系统的完整性.
说到清理内存,那么不得不提到 /proc 这一个虚拟文件系统,这里面的数据和文件都是内存中的实时数据,很多参数的获取都可以从下面相应的文件中得到。比如:
- 查看某一进程占用的内存大小和各项参数
- CPU 和主板的详细信息
- 显卡的参数等等
相应的关于内存的管理方式是在/proc/sys/vm/ drop_chches 文件中,一定要注意这个文件中存放的并不是具体的内存内容,而是 0-3 这几个数字,文件大小只有 1B ,而这些代号分别告诉系统代表不同的含义如下:
0:0 是系统默认值,默认情况下表示不释放内存,由操作系统自动管理
1:释放页缓存
2:释放dentries和inodes
3:释放所有缓存
举例
sync
echo 1 > /proc/sys/vm/drop_caches
echo 0 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
注意:在生产环境中的服务器我们不要频繁的去释放内存,只在必要时候清理内存即可,更重要的是我们应该从应用程序层面去优化内存的利用和释放,经常清理内存可能只是暂时屏蔽的应用程序中的一些bug,所以更重要的是程序的调优,其他的交给操作系统来管理。
英文说明
FREE(1) User Commands FREE(1)
NAME
free - Display amount of free and used memory in the system
SYNOPSIS
free [options]
DESCRIPTION
free displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches
used by the kernel. The information is gathered by parsing /proc/meminfo. The displayed columns are:
total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
used Used memory (calculated as total - free - buffers - cache)
free Unused memory (MemFree and SwapFree in /proc/meminfo)
shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available on kernels 2.6.32, displayed as zero if not avail‐
able)
buffers
Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)
buff/cache
Sum of buffers and cache
available
Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided
by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs
will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on
kernels 2.6.27+, otherwise the same as free)
OPTIONS
-b, --bytes
Display the amount of memory in bytes.
-k, --kilo
Display the amount of memory in kilobytes. This is the default.
-m, --mega
Display the amount of memory in megabytes.
-g, --giga
Display the amount of memory in gigabytes.
--tera Display the amount of memory in terabytes.
--peta Display the amount of memory in petabytes.
-h, --human
Show all output fields automatically scaled to shortest three digit unit and display the units of print out. Following units are used.
B = bytes
K = kilos
M = megas
G = gigas
T = teras
P = petas
If unit is missing, and you have exabyte of RAM or swap, the number is in terabytes and columns might not be aligned with header.
-w, --wide
Switch to the wide mode. The wide mode produces lines longer than 80 characters. In this mode buffers and cache are reported in two separate columns.
-c, --count count
Display the result count times. Requires the -s option.
-l, --lohi
Show detailed low and high memory statistics.
-s, --seconds seconds
Continuously display the result delay seconds apart. You may actually specify any floating point number for delay, usleep(3) is used for microsecond resolution delay times.
--si Use power of 1000 not 1024.
-t, --total
Display a line showing the column totals.
--help Print help.
-V, --version
Display version information.
FILES
/proc/meminfo
memory information
SEE ALSO
ps(1), slabtop(1), top(1), vmstat(8).
AUTHORS
Written by Brian Edmonds.
REPORTING BUGS
Please send bug reports to ⟨procps@freelists.org⟩
参考资料
linux manpage
手动释放linux内存cache
Linux free命令
free 命令详解
linux free命令详解(一)
Ubuntu 使用free命令used != total - free
Linux系统中的Page cache和Buffer cache
Linux 内核的文件 Cache 管理机制介绍

本文详细介绍Linux系统中的内存管理,包括free命令的使用方法及其参数解释。深入探讨内存使用情况的各项指标,如used、free、buffers、cache等,并解析Linux如何通过Pagecache和Buffercache提高I/O效率。
1195

被折叠的 条评论
为什么被折叠?



