Linux 基本工具(Memory Stat)
前面我们介绍过, 通过 top
或者 htop
命令能够查看服务器的物理机内存和 Swap
的使用情况, 除了这两种工具, 我们其实还会有些其他工具,完成类似的任务.
free
命令用于显示内存的使用信息, 比如
root@server:~# free -h
total used free shared buff/cache available
Mem: 3.9G 1.5G 1.1G 5.9M 1.3G 2.1G
Swap: 974M 0B 974M
除了类似 top
命令显示的 total
、 used
、 free
、 buff/cache
,还显示了 shared
、available
等信息。
这里有些内存概念需要我们知道:
- Cache 作为加载到内存中的磁盘数据,用于快速访问。
- Swap 是磁盘上的交换分区,被系统用来在物理内存紧张的情况下, 将使用频率低的内存页面交换到磁盘, 以便释放更多物理内存给系统使用。
更多free命令的内存可以查看 man free
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 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 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)
free
有些比较有用的选项:
-b
or--bytes
以字节为单位显示内存信息-k
or--kilo
以k为单位显示内存信息-m
or--mega
以m为单位显示内存信息-g
or--giga
以g为单位显示内存信息--tera
以t为单位显示内存信息-h
or--human
以便于人可读的信息显示内存信息