free
命令用于获取系统中可用的总随机存取内存量,已使用的内存量和系统中可用的交换内存量。free
命令或实用程序还显示内核使用的缓冲内存⁵。
free
命令在所有主流的Linux发行版中都是可用的,包括但不限于Debian、Ubuntu、Alpine、Arch Linux、Kali Linux、RedHat/CentOS、Fedora和Raspbian。在大多数系统中,free
命令是预装的,所以不需要安装。如果在某些系统中free
命令不可用,你可以尝试使用包管理器(如apt、yum或dnf)来安装procps
包,这个包包含free
命令。
对于Debian和Ubuntu,你可以使用以下命令来安装:
[linux@bashcommandnotfound.cn ~]$ sudo apt-get update
[linux@bashcommandnotfound.cn ~]$ sudo apt-get install procps
对于CentOS 7,你可以使用以下命令来安装:
[linux@bashcommandnotfound.cn ~]$ sudo yum update
[linux@bashcommandnotfound.cn ~]$ sudo yum install procps
对于CentOS 8和Fedora,你可以使用以下命令来安装:
[linux@bashcommandnotfound.cn ~]$ sudo dnf update
[linux@bashcommandnotfound.cn ~]$ sudo dnf install procps
free
命令的基本语法如下:
free [options]
你可以在[options]
字段中使用多个选项来改变free
命令的默认行为⁶。
以下是free
命令的一些常用选项:
选项 | 说明 |
---|---|
-b, --bytes | 以字节为单位显示内存。 |
-k, --kilo | 以千字节为单位显示内存(默认)。 |
-m, --mega | 以兆字节为单位显示内存。 |
-g, --giga | 以吉字节为单位显示内存。 |
–tera | 以太字节为单位显示内存。 |
-h, --human | 显示所有输出列,自动缩放到最短的三位数字单位,并显示单位。单位包括B(字节)、K(千字节)、M(兆字节)、G(吉字节)和T(太字节)。 |
-c, --count | 显示c次输出,此选项实际上与-s选项一起使用。 |
-l, --lohi | 显示详细的低和高内存统计信息。 |
-o, --old | 此选项禁用缓冲区调整行的显示。 |
-s, --seconds | 此选项允许你连续显示输出,每隔s秒延迟一次。实际上,usleep系统调用用于微秒分辨率的延迟时间。 |
-t, --total | 在输出中添加一个额外的行,显示列总计。 |
–help | 显示帮助信息并退出。 |
-V, --version | 显示版本信息并退出。 |
以下是一些free
命令的实例,这些实例可以帮助你更好地理解如何使用这个命令。
[linux@bashcommandnotfound.cn ~]$ free
这个命令会显示系统的内存使用情况。
[linux@bashcommandnotfound.cn ~]$ free -m
这个命令会以兆字节为单位显示系统的内存使用情况。
[linux@bashcommandnotfound.cn ~]$ free -g
这个命令会以吉字节为单位显示系统的内存使用情况。
[linux@bashcommandnotfound.cn ~]$ free -s 5
这个命令会每隔5秒显示一次系统的内存使用情况。
[linux@bashcommandnotfound.cn ~]$ free -t
这个命令会显示各行的总计字段:内存,交换空间和总计字段。
[linux@bashcommandnotfound.cn ~]$ free -h
此命令将显示人类可读的输出。例如,它将显示K,M或G表示千字节,兆字节或吉字节。
[linux@bashcommandnotfound.cn ~]$ free --wide
这个命令将显示详细的内存使用情况,它展示了缓存和缓冲区的详细使用情况。
[linux@bashcommandnotfound.cn ~]$ free -s 1 -c 1
此命令会在1秒后停止更新输出。
[linux@bashcommandnotfound.cn ~]$ free -l
这个命令会显示详细的低和高内存统计数据。
[linux@bashcommandnotfound.cn ~]$ free -b
这个命令会以bytes为单位显示,可以使输出结果更精确。
[linux@bashcommandnotfound.cn ~]$ free -k
此命令将以千字节为单位显示内存使用情况,是显示内存使用较为直观的一个单位。
free
命令显示的内存使用情况可能会让人感到困惑,特别是在理解“free”和“available”之间的区别时。bash: free: command not found
,请按照上面的指示安装procps
包。以下是一些与free
命令相关的命令: