(2)free详解 (每周一个linux命令系列)

2023-06-15,,

(2)free详解 (每周一个linux命令系列)

linux命令 free详解


引言:今天的命令是用来看内存的free

free

换一个套路,我们先看man free中对free的描述:


Display amount of free and used memory in the system

翻译:显示系统中使用的和未用的内存数量

我们再来看一下命令执行结果

              total        used        free      shared  buff/cache   available
Mem: 16131568 8461796 1418932 848140 6250840 6352668
Swap: 8191996 24 8191972

man free中对各个表头的解释如下

    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
available) buffers
Memory used by kernel buffers (Buffers in /proc/meminfo) cache Memory used by the page cache and slabs (Cached and Slab
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 pro‐
vided by the cache or free fields, this field takes into
account page cache and also that not all reclaimable mem‐
ory 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)

我们来分别解释一下(其实就是翻译一下),

free命令显示系统中所有使用的和未使用的物理内存和交换分区的数量(这里大概提一下,交换区作用是的当内存不够的时候,部分不常使用的信息被暂时存储到交换区,待使用的时候再放到内存中),以及内核使用的缓冲区和缓存(buffer和caches分不清的话,可以理解为buffer是一些放在内存中还没有刷到磁盘的缓存数据,而caches是从磁盘中读出到内存的数据。这里不展开介绍了),这些信息来源于解析 /proc/meminfo ,显示的属性如下:

 - total        系统安装的总内存
- used 使用的内存 计算方法为:total - free - buffers - cache
- free 未使用的内存
- shared (最可能是)被tmpfs文件系统使用的内存 一般不会用到
- buffers buffers使用的内存数
- cache cache和slab使用的内存数(slab是linux下的一种内存分配机制,可以避免频繁的分配同类内存,即可以循环利用)
- buff/cache buffers和cache的和
- available 估计有多少内存在启动新的应用时可以被用到

接下来我们学习一下free可携带的参数

Options:
-b, --bytes show output in bytes 使用字节显示
-k, --kilo show output in kilobytes 使用kb显示
-m, --mega show output in megabytes 使用mb显示
-g, --giga show output in gigabytes 使用gb显示
--tera show output in terabytes 使用tb
-h, --human show human-readable output 使用人类易读的方式
--si use powers of 1000 not 1024 使用一千字节计数
-l, --lohi show detailed low and high memory statistics 显示最高最低的统计信息
-t, --total show total for RAM + swap 显示物理内存和交换区总数
-s N, --seconds N repeat printing every N seconds 每几分钟重复显示一次
-c N, --count N repeat printing N times, then exit 重复输出n次然后退出(每秒一次)
-w, --wide wide output buffers和cache会分开显示 --help display this help and exit 帮助
-V, --version output version information and exit 版本信息

常用命令运行结果如下:

free -h
total used free shared buff/cache available
Mem: 15G 8.1G 1.2G 836M 6.1G 6.0G
Swap: 7.8G 24K 7.8G
dev1:~$ free -s 5
total used free shared buff/cache available
Mem: 16131568 8507812 1217156 856332 6406600 6299660
Swap: 8191996 24 8191972 ^C
dev1:~$ free -hs 5
total used free shared buff/cache available
Mem: 15G 8.1G 1.2G 836M 6.1G 6.0G
Swap: 7.8G 24K 7.8G ^C
dev1:~$ free -w
total used free shared buffers cache available
Mem: 16131568 8484328 1240332 856332 0 6406908 6323160
Swap: 8191996 24 8191972 dev1:~$ free -wh
total used free shared buffers cache available
Mem: 15G 8.1G 1.2G 836M 0B 6.1G 6.0G
Swap: 7.8G 24K 7.8G free -s 5 -c 2 //五秒运行一次,一共运行两次就退出
total used free shared buff/cache available
Mem: 16131568 8535816 1179516 856364 6416236 6271488
Swap: 8191996 24 8191972 total used free shared buff/cache available
Mem: 16131568 8539412 1175796 856364 6416360 6267892
Swap: 8191996 24 8191972

其他的运行结果就不给了,给了的话就有点刷文章长度的嫌疑。大概说一下常用的结合用法吧

    -h 命令是比较常用的 可以与其他的-w -s -c配合使用
    -s 和-c是可以同时使用的 例如 free -s 5 -c 2 //五秒运行一次,一共运行两次
    --si 是需要和-b -k -m -g这些一起使用,不再以1024为进位标准而是1000
    其他的请大家自由组合尝试吧

(2)free详解 (每周一个linux命令系列)的相关教程结束。

《(2)free详解 (每周一个linux命令系列).doc》

下载本文的Word格式文档,以方便收藏与打印。