vmstat

system monitoringLinux/Unix
The vmstat command is one of the most frequently used commands in Linux/Unix-like operating systems. vmstat Report virtual memory statistics

Quick Reference

Command Name:

vmstat

Category:

system monitoring

Platform:

Linux/Unix

Basic Usage:

vmstat [options] [arguments]

Common Use Cases

    Syntax

    vmstat [options] [delay [count]]

    Options

    Option Description
    -a, --active Display active and inactive memory
    -f, --forks Display number of forks since boot
    -m, --slabs Display slabinfo
    -n, --one-header Display header only once
    -s, --stats Display event counters and memory statistics
    -d, --disk Display disk statistics
    -D, --disk-sum Display disk summary
    -p, --partition device Display partition specific statistics
    -S, --unit character Define display unit (k, K, m, M) for size output
    -t, --timestamp Display timestamp with each update
    -w, --wide Use wide output format
    -V, --version Display version information
    -h, --help Display help information
    delay Delay between updates in seconds
    count Number of updates to display

    vmstat Output Fields

    Procs

    Field Description
    rNumber of processes waiting for run time
    bNumber of processes in uninterruptible sleep

    Memory

    Field Description
    swpdVirtual memory used (swap)
    freeIdle memory
    buffMemory used as buffers
    cacheMemory used as cache
    inactInactive memory (with -a option)
    activeActive memory (with -a option)

    Swap

    Field Description
    siMemory swapped in from disk (KB/s)
    soMemory swapped out to disk (KB/s)

    I/O

    Field Description
    biBlocks received from a block device (blocks/s)
    boBlocks sent to a block device (blocks/s)

    System

    Field Description
    inInterrupts per second, including the clock
    csContext switches per second

    CPU

    Field Description
    usTime spent running non-kernel code (user time, including nice time)
    syTime spent running kernel code (system time)
    idTime spent idle
    waTime spent waiting for IO
    stTime stolen from a virtual machine

    Examples

    How to Use These Examples

    The examples below show common ways to use the vmstat command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Display a snapshot of virtual memory statistics vmstat
    # Display statistics every 2 seconds for 5 times vmstat 2 5
    # Display statistics with unit suffixes (K, M, etc.) vmstat -S M
    # Show active and inactive memory vmstat -a
    # Display timestamp with each update vmstat -t 1 3
    # Display disk statistics vmstat -d # Display slab statistics vmstat -m # Show detailed statistics about page usage vmstat -p /dev/sda1 # Advanced Examples Advanced # Show memory statistics in megabytes vmstat -S M 1 5 # Display statistics with a wider format vmstat -w 1 3 # Combine active memory and timestamp options vmstat -at 2 3 # Display disk and memory statistics vmstat -d -a # Get detailed device information with verbose output vmstat -D # Display event counters and memory statistics vmstat -s # Report statistics in 1-second intervals ignoring zeros in output vmstat -n 1 # Show only the summary for the partitions vmstat -p ALL

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `vmstat` (Virtual Memory Statistics) command is a powerful system monitoring tool that provides information about processes, memory, paging, block IO, traps, and CPU activity. It's particularly useful for diagnosing system performance issues related to memory, CPU, or I/O bottlenecks. **Core Functionality:** 1. **Real-time System Monitoring**: vmstat provides snapshot or continuous monitoring of system resources. 2. **Memory Usage Analysis**: It reports detailed statistics about system memory usage, including physical memory, virtual memory (swap), buffers, and cache. 3. **Process State Monitoring**: vmstat displays the number of processes in various states, such as running or blocked. 4. **I/O Statistics**: It tracks block input/output operations, helping identify disk bottlenecks. 5. **CPU Utilization Breakdown**: The tool provides a percentage breakdown of CPU time spent in user space, kernel space, idle time, and waiting for I/O. **Common Use Cases:** 1. **Performance Troubleshooting**: System administrators use vmstat to diagnose performance issues by identifying resource bottlenecks. 2. **Memory Leak Detection**: Monitoring memory usage over time can help identify applications with memory leaks. 3. **Capacity Planning**: By monitoring system resource usage patterns, administrators can make informed decisions about hardware upgrades or resource allocation. 4. **I/O Bottleneck Identification**: High 'wa' (I/O wait) time in CPU statistics or high 'bi'/'bo' values indicate I/O bottlenecks. 5. **Swap Usage Monitoring**: Excessive swapping (high 'si'/'so' values) indicates insufficient physical memory, which can severely degrade system performance. **Interpreting vmstat Output:** 1. **Process States**: High 'r' values indicate CPU contention, while high 'b' values suggest I/O bottlenecks. 2. **Memory Fields**: Low 'free' memory combined with low 'cache' and 'buff' values may indicate memory pressure. 3. **Swap Activity**: Any non-zero 'si' and 'so' values warrant attention, as swapping significantly impacts performance. 4. **CPU States**: High 'us' indicates CPU-intensive applications, high 'sy' suggests kernel-intensive operations or syscall overhead, high 'wa' points to I/O bottlenecks, and high 'st' (in virtual environments) indicates CPU resources being taken by the hypervisor. **Best Practices:** 1. **Continuous Monitoring**: Use vmstat with a delay parameter (e.g., `vmstat 1`) to observe changes over time rather than relying on a single snapshot. 2. **Correlation with Other Tools**: Combine vmstat with other tools like `iostat`, `mpstat`, and `top` for a comprehensive view of system performance. 3. **Baseline Comparison**: Establish baseline readings during normal operation to better identify anomalies during troubleshooting. 4. **Unit Selection**: Use the `-S` option to select appropriate units (k, K, m, M) based on your system's memory size for easier reading. 5. **Field Selection**: Focus on relevant fields based on the issue you're troubleshooting rather than trying to analyze all fields simultaneously. **Advanced Features:** 1. **Disk Statistics**: The `-d` option provides detailed disk statistics, helping pinpoint which storage devices are under heavy load. 2. **Partition-Specific Information**: The `-p` option allows monitoring specific disk partitions. 3. **Slab Information**: The `-m` option displays kernel slab allocator statistics, useful for kernel memory debugging. 4. **System Event Counters**: The `-s` option shows various system event counters and memory statistics since boot. **Historical Context:** vmstat has been a core system monitoring tool in Unix-like operating systems for decades. While newer, more graphical monitoring tools exist, vmstat remains valuable due to its lightweight nature, consistent output format across distributions, and the ability to run in minimal environments like recovery consoles or over low-bandwidth connections. It's part of the procps or procps-ng package, which contains various utilities for accessing information from the /proc filesystem, providing a user-friendly interface to kernel data structures.

    Related Commands

    These commands are frequently used alongside vmstat or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the vmstat command works in different scenarios.

    $ vmstat
    View All Commands