Linux vmstat command
What is the vmstat command
vmstat (Virtual Memory Statistics) is a powerful performance monitoring tool on Linux systems; it reports key performance metrics in real time, including processes, memory, swap, I/O blocks, and CPU activity.
Core Features
- Memory monitoring: display the usage of physical memory and swap space
- CPU usage: show the proportions of user processes, system processes, and idle time
- Process status: report the number of running, waiting, and blocked processes
- I/O statistics: provide detailed information about block device read/write operations
Basic syntax and common parameters
Basic syntax
vmstat [选项] [时间间隔] [次数]
Common parameters
| Parameters | Description |
|---|---|
-a |
Display active and inactive memory |
-d |
Display disk statistics |
-p |
Display statistics for a specified disk partition |
-s |
Display a summary of memory-related statistics |
-m |
Display slab information |
-t |
Add timestamps to the output |
-S |
Display with specified units (k/K/m/M, representing 1000/1024/1000000/1048576 respectively) |
Detailed explanation of output fields
Executing vmstat After the command, the output is usually divided into the following sections:
1. Process information
procs
r: 运行队列中的进程数量
b: 等待 I/O 的进程数量
2. Memory information
memory
swpd: 使用的虚拟内存大小(KB)
free: 空闲的物理内存大小(KB)
buff: 用作缓冲区的内存大小(KB)
cache: 用作缓存的内存大小(KB)
3. Swap information
swap
si: 每秒从磁盘读入交换区的内存大小(KB/s)
so: 每秒写入磁盘的交换区内存大小(KB/s)
4. I/O information
io
bi: 每秒从块设备接收的块数(blocks/s)
bo: 每秒发送到块设备的块数(blocks/s)
5. System information
system
in: 每秒中断数(包括时钟中断)
cs: 每秒上下文切换次数
6. CPU usage
cpu
us: 用户进程占用 CPU 时间百分比
sy: 系统进程占用 CPU 时间百分比
id: 空闲 CPU 时间百分比
wa: 等待 I/O 的 CPU 时间百分比
st: 被虚拟机偷取的时间百分比
Practical example analysis
Example 1: basic monitoring (refreshes every 2 seconds)
Example
vmstat
2
Example output:
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 123456 78900 456789 0 0 5 12 345 6789 10 5 85 0 0
Example 2: display disk statistics
Example
vmstat
-d
The output shows read/write statistics for each disk, including the number of reads/writes and sectors.
Example 3: monitoring with timestamps (every 5 seconds, 3 times total)
Example
vmstat
-t
5
3
The output adds a timestamp before each line for easy recording and analysis.
Guide to diagnosing performance problems
1. Identifying CPU bottlenecks
- High us value: user processes consume a lot of CPU; the application may need optimization
- High sy value: the system kernel consumes a lot of CPU; there may be too many system calls
- High wa value: long I/O wait times, possibly a disk bottleneck
2. Diagnosing memory problems
- si/so persistently non-zero: indicates the system is using swap; physical memory may be insufficient
- free value keeps decreasing: possible memory leak
3. Process blocking problems
- large value in the b column: indicates multiple processes are waiting for I/O operations
Best Practice Recommendations
- Choosing the monitoring interval: in production, an interval of 5-10 seconds is recommended to avoid affecting system performance
- Long-term Monitoring: Combine with
nohupRedirect the output to a filenohup vmstat 10 360 > vmstat.log &
- Comprehensive analysis: Combine with
top,iostatand other tools - Benchmarking: record baseline data when the system is normal for comparison when problems occur
Frequently Asked Questions
Q1: What is the difference between the vmstat and top commands?
topprovides detailed process-level informationvmstatprovides an overview of overall system resource usage
Q2: What do high si/so values indicate?
It indicates the system frequently uses swap, possibly due to insufficient physical memory; consider adding memory or optimizing the application's memory usage.
Q3: How do I monitor a specific disk?
Use -p parameter to specify the disk partition:
Example
vmstat
-p
/
dev
/
sda1
By mastering the vmstat command, you will be able to quickly diagnose performance bottlenecks in Linux systems and provide data support for system optimization. It is recommended to use it together with other monitoring tools in real work to obtain a more comprehensive view of system performance.
Linux Command Reference