Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux vmstat command

Linux vmstat command

Linux Command Reference Linux Command Reference


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


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

2. Diagnosing memory problems

3. Process blocking problems


Best Practice Recommendations

  1. Choosing the monitoring interval: in production, an interval of 5-10 seconds is recommended to avoid affecting system performance
  2. Long-term Monitoring: Combine with nohup Redirect the output to a file
    nohup vmstat 10 360 > vmstat.log &
  3. Comprehensive analysis: Combine with top, iostat and other tools
  4. 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?

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 Linux Command Reference