# vmstat — Monitor Memory, Swap and CPU in Real Time

> Practical guide to vmstat — watch memory, swap, I/O wait and CPU load second by second and spot system bottlenecks early.

Source: https://www.jpkc.com/db/en/cheatsheets/shell-system/vmstat/

<!-- PROSE:intro -->
vmstat (virtual memory statistics) gives you the pulse of a Linux system in a single, compact line: process queues, memory, swap, block I/O and CPU usage at a glance. Part of the procps-ng package, it is a read-only command – perfect for quickly spotting memory pressure, swapping or I/O bottlenecks. One catch: when you run it with an interval (for example `vmstat 2`), the first line reports averages since boot – only the lines that follow reflect current activity.
<!-- PROSE:intro:end -->

## Basic Usage

`vmstat` — Show a single snapshot of system statistics since boot.

```bash
vmstat
```

`vmstat INTERVAL` — Show statistics every INTERVAL seconds, continuously.

```bash
vmstat 1
```

`vmstat INTERVAL COUNT` — Show statistics every INTERVAL seconds, COUNT times.

```bash
vmstat 1 10
```

`vmstat -w` — Wide output — wider columns for large values.

```bash
vmstat -w 1
```

`vmstat -t` — Append timestamp to each line.

```bash
vmstat -t 1
```

## Memory Statistics

`vmstat -s` — Show detailed memory statistics in a table format.

```bash
vmstat -s
```

`vmstat -S M` — Show values in megabytes instead of kilobytes.

```bash
vmstat -S M 1
```

`vmstat -S k` — Show values in kilobytes (default).

```bash
vmstat -S k 1
```

`vmstat -a` — Show active and inactive memory instead of buffer and cache.

```bash
vmstat -a 1
```

## Disk Statistics

`vmstat -d` — Show disk statistics: reads, writes, I/O time per device.

```bash
vmstat -d
```

`vmstat -D` — Show disk summary statistics.

```bash
vmstat -D
```

`vmstat -p PARTITION` — Show statistics for a specific partition.

```bash
vmstat -p sda1
```

## Other Options

`vmstat -f` — Show the number of forks (process creations) since boot.

```bash
vmstat -f
```

`vmstat -n` — Display the header only once (not periodically). Useful for logging.

```bash
vmstat -n 1 60 > vmstat.log
```

## Output Columns Explained

`r (procs)` — Number of runnable processes (waiting for CPU). High values = CPU contention.

`b (procs)` — Number of processes in uninterruptible sleep (usually waiting for I/O).

`swpd (memory)` — Amount of swap memory used (KB). Non-zero = memory pressure.

`free (memory)` — Amount of idle/free memory (KB).

`buff (memory)` — Amount of memory used as kernel buffers (KB).

`cache (memory)` — Amount of memory used as page cache (KB).

`si (swap)` — Memory swapped in from disk per second (KB/s). Non-zero = active swapping.

`so (swap)` — Memory swapped out to disk per second (KB/s). Non-zero = memory pressure.

`bi (io)` — Blocks received from block devices per second.

`bo (io)` — Blocks sent to block devices per second.

`in (system)` — Interrupts per second.

`cs (system)` — Context switches per second.

`us (cpu)` — Time spent running user code (%).

`sy (cpu)` — Time spent running kernel/system code (%).

`id (cpu)` — Idle time (%). Low = CPU busy.

`wa (cpu)` — Time waiting for I/O (%). High = I/O bottleneck.

`st (cpu)` — Time stolen by hypervisor (%). Non-zero = VM resource contention.

## Common Patterns

`vmstat -w 1` — Real-time system overview with wide columns. The go-to command.

```bash
vmstat -w 1
```

`vmstat -S M -w 1` — Real-time monitoring with values in megabytes.

```bash
vmstat -S M -w 1
```

`vmstat 1 | awk '$7 > 0 || $8 > 0'` — Watch for swap activity (si/so columns non-zero).

```bash
vmstat 1 | awk '$7 > 0 || $8 > 0'
```

`vmstat -n -t 1 3600 > vmstat.log` — Log system stats for one hour (1 sample/sec).

```bash
vmstat -n -t 1 3600 > vmstat.log
```

`vmstat 1 | awk 'NR>2 && $1>4 {print "CPU overloaded: r="$1}'` — Alert when runnable processes exceed CPU count.

```bash
vmstat 1 | awk 'NR>2 && $1>4 {print "CPU overloaded: r="$1}'
```

<!-- PROSE:outro -->
## Conclusion

vmstat is one of the fastest diagnostic tools around: one command, one line, and you know whether your system is under memory, CPU or I/O pressure. Remember the key columns: sustained non-zero `si`/`so` means real memory pressure from swapping, a persistently high `r` queue points to a CPU shortage and high `wa` signals an I/O bottleneck. Always pass an interval such as `vmstat 2` and ignore the first line, since it only reports the average since boot. For per-device or per-process detail, pair vmstat with `iostat`, `free` and `top`.

## Further Reading

- [manpages.debian.org: vmstat(8)](https://manpages.debian.org/bookworm/procps/vmstat.8.en.html) – the official manual page from the procps package
- [Brendan Gregg: Linux Performance](https://www.brendangregg.com/linuxperf.html) – widely referenced overview of Linux performance-analysis tools including vmstat
<!-- PROSE:outro:end -->

## Related Commands

- [iostat](https://www.jpkc.com/db/en/cheatsheets/shell-system/iostat/) – detailed per-device disk and I/O utilisation
- [free](https://www.jpkc.com/db/en/cheatsheets/shell-system/free/) – quickly show free and used memory
- [top](https://www.jpkc.com/db/en/cheatsheets/shell-system/top/) – watch processes and resource usage in real time

