free命令常用参数详解

时间:2022-04-18 02:29:26

       free命令常用参数详解及常用内存工具介绍

                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.内存空间使用状态

free命令常用参数详解

1>."-b"参数(以字节为单位显示内存使用情况)

[root@node101.yinzhengjie.org.cn ~]# free -b
total used free shared buff/cache available
Mem:
Swap:
[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:
Swap:
[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:
Swap:
[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:
Swap:
[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: .6G 131M .2G 8.6M 288M .3G
Swap: .9G 0B .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
total used free shared buff/cache available
Mem: .6G 132M .2G 8.6M 288M .3G
Swap: .9G 0B .9G total used free shared buff/cache available
Mem: .6G 132M .2G 8.6M 288M .3G
Swap: .9G 0B .9G total used free shared buff/cache available
Mem: .6G 132M .2G 8.6M 288M .3G
Swap: .9G 0B .9G total used free shared buff/cache available
Mem: .6G 132M .2G 8.6M 288M .3G
Swap: .9G 0B .9G total used free shared buff/cache available
Mem: .6G 132M .2G 8.6M 288M .3G
Swap: .9G 0B .9G total used free shared buff/cache available
Mem: .6G 132M .2G 8.6M 288M .3G
Swap: .9G 0B .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
total used free shared buff/cache available
Mem: .6G 131M .2G 8.6M 288M .3G
Swap: .9G 0B .9G
Total: 15G 131M 15G total used free shared buff/cache available
Mem: .6G 131M .2G 8.6M 288M .3G
Swap: .9G 0B .9G
Total: 15G 131M 15G total used free shared buff/cache available
Mem: .6G 131M .2G 8.6M 288M .3G
Swap: .9G 0B .9G
Total: 15G 131M 15G total used free shared buff/cache available
Mem: .6G 131M .2G 8.6M 288M .3G
Swap: .9G 0B .9G
Total: 15G 131M 15G total used free shared buff/cache available
Mem: .6G 131M .2G 8.6M 288M .3G
Swap: .9G 0B .9G
Total: 15G 131M 15G total used free shared buff/cache available
Mem: .6G 131M .2G 8.6M 288M .3G
Swap: .9G 0B .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”的这一列,新版本已经移除了该选项),

 [root@yinzhengjie ~]# free -t  -o -m -s
total used free shared buffers cached
Mem:
Swap:
Total: total used free shared buffers cached
Mem:
Swap:
Total: total used free shared buffers cached
Mem:
Swap:
Total: total used free shared buffers cached
Mem:
Swap:
Total: total used free shared buffers cached
Mem:
Swap:
Total: ^C
[root@yinzhengjie ~]#

free -t -o -m -s 1

9>."-C"参数(刷新n次后即退出)

[root@node101.yinzhengjie.org.cn ~]# free -t -c  -m -s
total used free shared buff/cache available
Mem:
Swap:
Total: total used free shared buff/cache available
Mem:
Swap:
Total: total used free shared buff/cache available
Mem:
Swap:
Total:
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# free -t -c 3 -m -s 1

10>.输出结果说明

                     
   total used free shared buffers cached #第一行
Mem:    #第二行
-/+ buffers/cache:   #第三行
Swap:    #第四行 我们知道这个是"free -m"的输出结果,我只不过是将结果取出来了,然后进行标号,用坐标的方式来讲解如何查看内容:
第一行:
  total(,):总计物理内存的大小。
  used(,):已使用的空间。
  free(,):可用的空间。
  shared(,):多个进程共享的内存总额。现在已经deprecated,其值总是0(当然在一些系统上也可能不是0,主要取决于free命令是怎么实现的)。
  buffers(,):用于存放要输出到disk(块设备)的数据的,[A buffer is something that has yet to be "written" to disk. ]即提高了内存网硬盘加速写的能力
  cached(,):存放从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物理内存;
  (,):7718MB已经被使用了;
  (,):137MB可用被使用。
  因此,我们可以得到一个公式哟:(3.3)= (,)+ (,) 第三行:(是从一个应用程序的角度看系统内存的使用情况)
  (,,):即-buffers/cache,表示一个应用程序认为系统被用掉多少内存;
  (,):即+buffers/cache,表示一个应用程序认为系统还有多少内存;
因此我们又可以得到2个公式哟:
>.(,) = (,) - (,) - (,)
>.(,) = (,) + (,) - (,)
第四行:
  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 [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
K total memory
K used memory
K active memory
K inactive memory
K free memory
K buffer memory
K swap cache
K total swap
K used swap
K free swap
non-nice user cpu ticks
nice user cpu ticks
system cpu ticks
idle cpu ticks
IO-wait cpu ticks
IRQ cpu ticks
softirq cpu ticks
stolen cpu ticks
pages paged in
pages paged out
pages swapped in
pages swapped out
interrupts
CPU context switches
boot time
forks
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# vmstat -s

2>.动态观察虚拟内存情况

[root@node101.yinzhengjie.org.cn ~]# vmstat  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 [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: .6G 130M .2G 8.6M 288M .3G
Swap: .9G 0B .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: .6G 125M .2G 8.6M 288M .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: .6G 130M .2G 8.6M 288M .3G
Swap: .9G 0B .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 :10.1.-.el7 will be installed
--> Processing Dependency: libsensors.so.()(64bit) for package: sysstat-10.1.-.el7.x86_64
--> Running transaction check
---> Package lm_sensors-libs.x86_64 :3.4.-.20160601gitf9185e5.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved =======================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================
Installing:
sysstat x86_64 10.1.-.el7 base k
Installing for dependencies:
lm_sensors-libs x86_64 3.4.-.20160601gitf9185e5.el7 base k Transaction Summary
=======================================================================================================================================
Install Package (+ Dependent package) Total download size: k
Installed size: 1.2 M
Downloading packages:
(/): lm_sensors-libs-3.4.-.20160601gitf9185e5.el7.x86_64.rpm | kB ::
(/): sysstat-10.1.-.el7.x86_64.rpm | kB ::
---------------------------------------------------------------------------------------------------------------------------------------
Total kB/s | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lm_sensors-libs-3.4.-.20160601gitf9185e5.el7.x86_64 /
Installing : sysstat-10.1.-.el7.x86_64 /
Verifying : lm_sensors-libs-3.4.-.20160601gitf9185e5.el7.x86_64 /
Verifying : sysstat-10.1.-.el7.x86_64 / Installed:
sysstat.x86_64 :10.1.-.el7 Dependency Installed:
lm_sensors-libs.x86_64 :3.4.-.20160601gitf9185e5.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install sysstat       #需要安装该软件包才能使用

[root@node101.yinzhengjie.org.cn ~]# iostat  3          #每秒查询1次,共计查询3次
Linux 3.10.-.el7.x86_64 (node101.yinzhengjie.org.cn) // _x86_64_ ( 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
dm- 5.02 35.74 1991.30
dm- 0.03 0.93 0.00
dm- 0.02 0.96 0.38 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
dm- 329.00 0.00 168448.00
dm- 0.00 0.00 0.00
dm- 0.00 0.00 0.00 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
dm- 545.00 0.00 279040.00
dm- 0.00 0.00 0.00
dm- 0.00 0.00 0.00 [root@node101.yinzhengjie.org.cn ~]#

2>.pmap命令(进程对应的内存映射)

[root@node101.yinzhengjie.org.cn ~]# pidof dd

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /proc/`pidof dd`/maps
- r-xp fd: /usr/bin/dd
- r--p fd: /usr/bin/dd
- rw-p fd: /usr/bin/dd
0152c000-0154d000 rw-p : [heap]
7f36062e0000-7f360c80a000 r--p fd: /usr/lib/locale/locale-archive
7f360c80a000-7f360c9cc000 r-xp fd: /usr/lib64/libc-2.17.so
7f360c9cc000-7f360cbcc000 ---p 001c2000 fd: /usr/lib64/libc-2.17.so
7f360cbcc000-7f360cbd0000 r--p 001c2000 fd: /usr/lib64/libc-2.17.so
7f360cbd0000-7f360cbd2000 rw-p 001c6000 fd: /usr/lib64/libc-2.17.so
7f360cbd2000-7f360cbd7000 rw-p :
7f360cbd7000-7f360cbf9000 r-xp fd: /usr/lib64/ld-2.17.so
7f360cdec000-7f360cdef000 rw-p :
7f360cdf7000-7f360cdf8000 rw-p :
7f360cdf8000-7f360cdf9000 r--p fd: /usr/lib64/ld-2.17.so
7f360cdf9000-7f360cdfa000 rw-p fd: /usr/lib64/ld-2.17.so
7f360cdfa000-7f360cdfb000 rw-p :
7fff6d857000-7fff6d878000 rw-p : [stack]
7fff6d9ce000-7fff6d9d0000 r-xp : [vdso]
ffffffffff600000-ffffffffff601000 r-xp : [vsyscall]
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pmap `pidof dd`
: dd if=/dev/zero of=bigfile.txt
68K r-x-- dd
4K r---- dd
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
- r-xp fd: /usr/bin/dd
- r--p fd: /usr/bin/dd
- rw-p fd: /usr/bin/dd
- rw-p : [heap]
7f4886004000-7f488c52e000 r--p fd: /usr/lib/locale/locale-archive
7f488c52e000-7f488c6f0000 r-xp fd: /usr/lib64/libc-2.17.so
7f488c6f0000-7f488c8f0000 ---p 001c2000 fd: /usr/lib64/libc-2.17.so
7f488c8f0000-7f488c8f4000 r--p 001c2000 fd: /usr/lib64/libc-2.17.so
7f488c8f4000-7f488c8f6000 rw-p 001c6000 fd: /usr/lib64/libc-2.17.so
7f488c8f6000-7f488c8fb000 rw-p :
7f488c8fb000-7f488c91d000 r-xp fd: /usr/lib64/ld-2.17.so
7f488cb10000-7f488cb13000 rw-p :
7f488cb1b000-7f488cb1c000 rw-p :
7f488cb1c000-7f488cb1d000 r--p fd: /usr/lib64/ld-2.17.so
7f488cb1d000-7f488cb1e000 rw-p fd: /usr/lib64/ld-2.17.so
7f488cb1e000-7f488cb1f000 rw-p :
7ffe3fad1000-7ffe3faf2000 rw-p : [stack]
7ffe3fbfc000-7ffe3fbfe000 r-xp : [vdso]
ffffffffff600000-ffffffffff601000 r-xp : [vsyscall]
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# cat /proc/`pidof dd`/maps      #易读性较差