free命令常用参数详解及常用内存工具介绍
分类:
IT文章
•
2022-08-01 22:21:20
free命令常用参数详解及常用内存工具介绍
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.内存空间使用状态
1>."-b"参数(以字节为单位显示内存使用情况)
[root@node101.yinzhengjie.org.cn ~]# free -b
total used free shared buff/cache available
Mem: 8201146368 138366976 7760576512 8986624 302202880 7797694464
Swap: 8455712768 0 8455712768
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -b
2>."-k"参数(默认选项,以“kb”为单位显示内存使用情况)
[root@node101.yinzhengjie.org.cn ~]# free -k
total used free shared buff/cache available
Mem: 8008932 135112 7578680 8776 295140 7614948
Swap: 8257532 0 8257532
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -k
3.“-m”参数(以“mb”为单位显示内存使用情况)
[root@node101.yinzhengjie.org.cn ~]# free -m
total used free shared buff/cache available
Mem: 7821 132 7400 8 288 7436
Swap: 8063 0 8063
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -m
4>.“-g”参数(以"gb"为单位显示内存使用情况)
[root@node101.yinzhengjie.org.cn ~]# free -g
total used free shared buff/cache available
Mem: 7 0 7 0 0 7
Swap: 7 0 7
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -g
5>.以易读格式显示
[root@node101.yinzhengjie.org.cn ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 131M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -h
6>.“-s”参数(后面需要跟个数字,单位是"s",显示每隔多少秒数来显示一次内存使用情况)
[root@node101.yinzhengjie.org.cn ~]# free -h -s 1
total used free shared buff/cache available
Mem: 7.6G 132M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.6G 132M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.6G 132M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.6G 132M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.6G 132M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.6G 132M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
^C
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -h -s 1
7>.“-t”参数(在末尾活多出一行“Total”,表示显示总和列)
[root@node101.yinzhengjie.org.cn ~]# free -t -h -s 1
total used free shared buff/cache available
Mem: 7.6G 131M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
Total: 15G 131M 15G
total used free shared buff/cache available
Mem: 7.6G 131M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
Total: 15G 131M 15G
total used free shared buff/cache available
Mem: 7.6G 131M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
Total: 15G 131M 15G
total used free shared buff/cache available
Mem: 7.6G 131M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
Total: 15G 131M 15G
total used free shared buff/cache available
Mem: 7.6G 131M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
Total: 15G 131M 15G
total used free shared buff/cache available
Mem: 7.6G 131M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
Total: 15G 131M 15G
^C
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -t -h -s 1
8.“-o”参数(不显示包含“-/+ buffers/cache”的这一列,新版本已经移除了该选项),
1 [root@yinzhengjie ~]# free -t -o -m -s 1
2 total used free shared buffers cached
3 Mem: 7855 7724 131 0 152 6352
4 Swap: 7983 58 7925
5 Total: 15839 7782 8057
6
7 total used free shared buffers cached
8 Mem: 7855 7724 131 0 152 6352
9 Swap: 7983 58 7925
10 Total: 15839 7782 8056
11
12 total used free shared buffers cached
13 Mem: 7855 7724 131 0 152 6352
14 Swap: 7983 58 7925
15 Total: 15839 7782 8056
16
17 total used free shared buffers cached
18 Mem: 7855 7724 131 0 152 6352
19 Swap: 7983 58 7925
20 Total: 15839 7782 8056
21
22 total used free shared buffers cached
23 Mem: 7855 7724 131 0 152 6352
24 Swap: 7983 58 7925
25 Total: 15839 7782 8057
26
27 ^C
28 [root@yinzhengjie ~]#
free -t -o -m -s 1
9>."-C"参数(刷新n次后即退出)
[root@node101.yinzhengjie.org.cn ~]# free -t -c 3 -m -s 1
total used free shared buff/cache available
Mem: 7821 131 7401 8 288 7437
Swap: 8063 0 8063
Total: 15885 131 15465
total used free shared buff/cache available
Mem: 7821 131 7401 8 288 7437
Swap: 8063 0 8063
Total: 15885 131 15465
total used free shared buff/cache available
Mem: 7821 131 7401 8 288 7437
Swap: 8063 0 8063
Total: 15885 131 15465
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -t -c 3 -m -s 1
10>.输出结果说明
1 2 3 4 5 6 7 8
2 total used free shared buffers cached #第一行
3 Mem: 7855 7718 137 0 152 6348 #第二行
4 -/+ buffers/cache: 1218 6637 #第三行
5 Swap: 7983 58 7925 #第四行
我们知道这个是"free -m"的输出结果,我只不过是将结果取出来了,然后进行标号,用坐标的方式来讲解如何查看内容:
第一行:
total(2,3):总计物理内存的大小。
used(2,4):已使用的空间。
free(2,5):可用的空间。
shared(2,6):多个进程共享的内存总额。现在已经deprecated,其值总是0(当然在一些系统上也可能不是0,主要取决于free命令是怎么实现的)。
buffers(2,7):用于存放要输出到disk(块设备)的数据的,[A buffer is something that has yet to be "written" to disk. ]即提高了内存网硬盘加速写的能力
cached(2,8):存放从disk上读出的数据,['A cache is something that has been "read" from the disk and stored for later use.'] (cached往往比buffers数字大就是为了提高IO read的性能)即提高了加速读的能力
第二行:(输出时从操作系统(OS)来看的)
看了第一行的解释,我能知道了第一行输出时从操作系统(OS)来看的。也就是说,从OS的角度来看
(3.3):即7855MB物理内存;
(3,4):7718MB已经被使用了;
(3,5):137MB可用被使用。
因此,我们可以得到一个公式哟:(3.3)= (3,4)+ (3,5)
第三行:(是从一个应用程序的角度看系统内存的使用情况)
(4,4,):即-buffers/cache,表示一个应用程序认为系统被用掉多少内存;
(4,5):即+buffers/cache,表示一个应用程序认为系统还有多少内存;
因此我们又可以得到2个公式哟:
1>.(4,4) = (3,4) - (3,7) - (3,8)
2>.(4,5) = (3,5) + (3,7) - (3,8)
第四行:
free的输出一共有四行,第四行为交换区的信息,分别是交换的总量(total),使用量(used)和有多少空闲的交换区(free)
温馨提示:
free命令由procps.*.rpm提供(在Redhat系列的OS上)。free命令的所有输出值都是从/proc/meminfo中读出的。“-/+ buffers/cache”其对应的used表示实际使用内存,对应的free对应的才是实际空闲的内存。
二.vmstat命令(查看虚拟内存信息)
[root@node101.yinzhengjie.org.cn ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 7579548 2108 293188 0 0 16 4 10 11 0 0 100 0 0
[root@node101.yinzhengjie.org.cn ~]#
参数说明:
procs:
r:
可运行(正运行或等待运行)进程的个数,和核心数有关
b:
处于不可中断睡眠态的进程个数(被阻塞的队列的长度)
memory:
swpd:
交换内存的使用总量
free:
空闲物理内存总量
buffer:
用于buffer的内存总量
cache:
用于cache的内存总量
swap:
si:
从磁盘交换进内存的数据速率(kb/s)
so:
从内存交换至磁盘的数据速率(kb/s)
io:
bi:
从块设备读入数据到系统的速率(kb/s)
bo:
保存数据至块设备的速率
system:
in:
interrupts 中断速率,包括时钟
cs:
context switch 进程切换速率
cpu:
us:
Time spent running non-kernel code
sy:
Time spent running kernel code
id:
Time spent idle. Linux 2.5.41前,包括IO-wait time.
wa:
Time spent waiting for IO. 2.5.41前,包括in idle.
st:
Time stolen from a virtual machine. 2.6.11前, unknown.
1>."-s" 参数显示内存的统计数据
[root@node101.yinzhengjie.org.cn ~]# vmstat -s
8008932 K total memory
133940 K used memory
192536 K active memory
90568 K inactive memory
7579696 K free memory
2108 K buffer memory
293188 K swap cache
8257532 K total swap
0 K used swap
8257532 K free swap
764 non-nice user cpu ticks
0 nice user cpu ticks
600 system cpu ticks
1520500 idle cpu ticks
107 IO-wait cpu ticks
0 IRQ cpu ticks
97 softirq cpu ticks
0 stolen cpu ticks
230873 pages paged in
54698 pages paged out
0 pages swapped in
0 pages swapped out
142591 interrupts
159486 CPU context switches
1574064299 boot time
4008 forks
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# vmstat -s
2>.动态观察虚拟内存情况
[root@node101.yinzhengjie.org.cn ~]# vmstat 1 10 #指定每秒查看一次虚拟内存的情况,总共查询10次。
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 2750480 2108 5120208 0 0 12 405 35 13 0 1 99 0 0
0 0 0 2750640 2108 5120208 0 0 0 0 49 42 0 0 100 0 0
0 0 0 2750492 2108 5120208 0 0 0 0 16 16 0 0 100 0 0
0 1 0 2748380 2108 5122456 0 0 0 232448 516 52 0 1 95 5 0
1 0 0 6688976 2108 1182236 0 0 0 114692 2817 381 2 21 73 4 0
1 0 0 6346416 2108 1524552 0 0 0 0 1150 31 5 20 75 0 0
1 1 0 6008240 2108 1862864 0 0 0 88064 1227 20 4 21 75 1 0
1 0 0 5682800 2108 2188116 0 0 0 239616 2414 59 5 23 66 5 0
1 0 0 5362324 2108 2508880 0 0 0 327680 2554 56 5 23 66 6 0
1 1 0 5049672 2108 2821640 0 0 0 592384 3234 131 5 26 51 17 0
[root@node101.yinzhengjie.org.cn ~]#
3>.用于测试内存的相关命令
[root@node101.yinzhengjie.org.cn ~]# dd if=/dev/zero of=/dev/null bs=2G #从内存到内存
[root@node101.yinzhengjie.org.cn ~]# dd if=/dev/sda of=/dev/null #从磁盘读数据到内存
[root@node101.yinzhengjie.org.cn ~]# dd if=/dev/zero of=bigfile.txt #从内存写数据到磁盘(有可能会先放入Buffer)
三.交换分区管理
[root@node101.yinzhengjie.org.cn ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 130M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# swapoff /dev/mapper/centos-swap #禁用交换内存
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 125M 7.2G 8.6M 288M 7.3G
Swap: 0B 0B 0B
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# swapon /dev/mapper/centos-swap #启用交换内存
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 130M 7.2G 8.6M 288M 7.3G
Swap: 7.9G 0B 7.9G
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
四.内存工具介绍
1>.iostat(统计CPU和设备IO信息)
[root@node101.yinzhengjie.org.cn ~]# yum -y install sysstat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package sysstat.x86_64 0:10.1.5-18.el7 will be installed
--> Processing Dependency: libsensors.so.4()(64bit) for package: sysstat-10.1.5-18.el7.x86_64
--> Running transaction check
---> Package lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================
Installing:
sysstat x86_64 10.1.5-18.el7 base 315 k
Installing for dependencies:
lm_sensors-libs x86_64 3.4.0-8.20160601gitf9185e5.el7 base 42 k
Transaction Summary
=======================================================================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 357 k
Installed size: 1.2 M
Downloading packages:
(1/2): lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64.rpm | 42 kB 00:00:05
(2/2): sysstat-10.1.5-18.el7.x86_64.rpm | 315 kB 00:00:07
---------------------------------------------------------------------------------------------------------------------------------------
Total 51 kB/s | 357 kB 00:00:07
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64 1/2
Installing : sysstat-10.1.5-18.el7.x86_64 2/2
Verifying : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64 1/2
Verifying : sysstat-10.1.5-18.el7.x86_64 2/2
Installed:
sysstat.x86_64 0:10.1.5-18.el7
Dependency Installed:
lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7
Complete!
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# yum -y install sysstat #需要安装该软件包才能使用
[root@node101.yinzhengjie.org.cn ~]# iostat 1 3 #每秒查询1次,共计查询3次
Linux 3.10.0-957.el7.x86_64 (node101.yinzhengjie.org.cn) 11/18/2019 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.09 0.00 0.84 0.05 0.00 99.01
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 5.45 43.10 1992.06 234617 10843904
dm-0 5.02 35.74 1991.30 194527 10839808
dm-1 0.03 0.93 0.00 5052 0
dm-2 0.02 0.96 0.38 5250 2048
avg-cpu: %user %nice %system %iowait %steal %idle
4.58 0.00 21.37 2.80 0.00 71.25
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 171.00 0.00 87552.00 0 87552
dm-0 329.00 0.00 168448.00 0 168448
dm-1 0.00 0.00 0.00 0 0
dm-2 0.00 0.00 0.00 0 0
avg-cpu: %user %nice %system %iowait %steal %idle
4.80 0.00 23.20 4.00 0.00 68.00
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 559.00 0.00 286208.00 0 286208
dm-0 545.00 0.00 279040.00 0 279040
dm-1 0.00 0.00 0.00 0 0
dm-2 0.00 0.00 0.00 0 0
[root@node101.yinzhengjie.org.cn ~]#
2>.pmap命令(进程对应的内存映射)
[root@node101.yinzhengjie.org.cn ~]# pidof dd
4350
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /proc/`pidof dd`/maps
00400000-00411000 r-xp 00000000 fd:00 2393 /usr/bin/dd
00610000-00611000 r--p 00010000 fd:00 2393 /usr/bin/dd
00611000-00612000 rw-p 00011000 fd:00 2393 /usr/bin/dd
0152c000-0154d000 rw-p 00000000 00:00 0 [heap]
7f36062e0000-7f360c80a000 r--p 00000000 fd:00 67164275 /usr/lib/locale/locale-archive
7f360c80a000-7f360c9cc000 r-xp 00000000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f360c9cc000-7f360cbcc000 ---p 001c2000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f360cbcc000-7f360cbd0000 r--p 001c2000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f360cbd0000-7f360cbd2000 rw-p 001c6000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f360cbd2000-7f360cbd7000 rw-p 00000000 00:00 0
7f360cbd7000-7f360cbf9000 r-xp 00000000 fd:00 34070969 /usr/lib64/ld-2.17.so
7f360cdec000-7f360cdef000 rw-p 00000000 00:00 0
7f360cdf7000-7f360cdf8000 rw-p 00000000 00:00 0
7f360cdf8000-7f360cdf9000 r--p 00021000 fd:00 34070969 /usr/lib64/ld-2.17.so
7f360cdf9000-7f360cdfa000 rw-p 00022000 fd:00 34070969 /usr/lib64/ld-2.17.so
7f360cdfa000-7f360cdfb000 rw-p 00000000 00:00 0
7fff6d857000-7fff6d878000 rw-p 00000000 00:00 0 [stack]
7fff6d9ce000-7fff6d9d0000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pmap `pidof dd`
4350: dd if=/dev/zero of=bigfile.txt
0000000000400000 68K r-x-- dd
0000000000610000 4K r---- dd
0000000000611000 4K rw--- dd
000000000152c000 132K rw--- [ anon ]
00007f36062e0000 103592K r---- locale-archive
00007f360c80a000 1800K r-x-- libc-2.17.so
00007f360c9cc000 2048K ----- libc-2.17.so
00007f360cbcc000 16K r---- libc-2.17.so
00007f360cbd0000 8K rw--- libc-2.17.so
00007f360cbd2000 20K rw--- [ anon ]
00007f360cbd7000 136K r-x-- ld-2.17.so
00007f360cdec000 12K rw--- [ anon ]
00007f360cdf7000 4K rw--- [ anon ]
00007f360cdf8000 4K r---- ld-2.17.so
00007f360cdf9000 4K rw--- ld-2.17.so
00007f360cdfa000 4K rw--- [ anon ]
00007fff6d857000 132K rw--- [ stack ]
00007fff6d9ce000 8K r-x-- [ anon ]
ffffffffff600000 4K r-x-- [ anon ]
total 108000K
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pmap `pidof dd` #读取效果要比上面的命令要好的多
3>.进程对应的内存映射的另外一种实现
[root@node101.yinzhengjie.org.cn ~]# pidof dd
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /proc/`pidof dd`/maps
00400000-00411000 r-xp 00000000 fd:00 2393 /usr/bin/dd
00610000-00611000 r--p 00010000 fd:00 2393 /usr/bin/dd
00611000-00612000 rw-p 00011000 fd:00 2393 /usr/bin/dd
01814000-01835000 rw-p 00000000 00:00 0 [heap]
7f4886004000-7f488c52e000 r--p 00000000 fd:00 67164275 /usr/lib/locale/locale-archive
7f488c52e000-7f488c6f0000 r-xp 00000000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f488c6f0000-7f488c8f0000 ---p 001c2000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f488c8f0000-7f488c8f4000 r--p 001c2000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f488c8f4000-7f488c8f6000 rw-p 001c6000 fd:00 33561967 /usr/lib64/libc-2.17.so
7f488c8f6000-7f488c8fb000 rw-p 00000000 00:00 0
7f488c8fb000-7f488c91d000 r-xp 00000000 fd:00 34070969 /usr/lib64/ld-2.17.so
7f488cb10000-7f488cb13000 rw-p 00000000 00:00 0
7f488cb1b000-7f488cb1c000 rw-p 00000000 00:00 0
7f488cb1c000-7f488cb1d000 r--p 00021000 fd:00 34070969 /usr/lib64/ld-2.17.so
7f488cb1d000-7f488cb1e000 rw-p 00022000 fd:00 34070969 /usr/lib64/ld-2.17.so
7f488cb1e000-7f488cb1f000 rw-p 00000000 00:00 0
7ffe3fad1000-7ffe3faf2000 rw-p 00000000 00:00 0 [stack]
7ffe3fbfc000-7ffe3fbfe000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /proc/`pidof dd`/maps #易读性较差