Introduction
The commandvmstat(virtual memory statistics) is an essential tool for system managers who need to get a quick and continuous view of the performance of a Linux computer. It provides information on processes, memory, pagation, I / O blocks and CPU activity, all in a single output line that is updated at user-defined intervals.
Basic syntax
The simplest way to runvmstatis:
vmstat [retard] [counter]
Whereretardois the number of seconds between each sample andcontadorindicates how many samples will be shown before completion. If the counter is omitted, the command is run indefinitely until it is interrupted withCtrl + C.
Description of columns
The output ofvmstatis divided into several blocks. Each block contains columns representing specific metrics:
- pros:
r (procesos en espera de ejecución) yb (procesos bloqueados en I/O no interrumpible). - memory:
swpd (memoria swap usada),free (memoria libre),buff (memoria usada como buffers) ycache (memoria usada como caché). - swap:
si (swap in: bloques leídos desde swap por segundo) yso (swap out: bloques escritos a swap por segundo). - o:
bi (bloques recibidos desde un dispositivo de bloque por segundo) ybo (bloques enviados a un dispositivo de bloque por segundo). - system:
in (interrupciones por segundo) ycs (cambios de contexto por segundo). - cpu: CPU time rates used in
us (usuario),sy (sistema),id (inactivo),wa (espera de I/O) yst (tiempo robado por hipervisor).
Examples of practical use
To observe system activity every 2 seconds and show 5 samples:
vmstat 2 5
This will produce a table where each row corresponds to a 2-second interval, allowing to detect consumption peaks or behavior patterns.
If you want to focus only on memory and pagination, you can combinevmstatwithawkto extract specific columns:
vmstat 1 | awk '{print $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20} '
In this example the columns of memory, swap, I / O and CPU are printed in each second.
Useful options
-a: shows active and inactive memory instead of buffers and cache.-f: shows the number of processes created from system start.-m: shows memory use information by the kernel allocator (slab).-s: shows a table of different event counters (interruptions, context changes, etc.).-d: shows I / O disk statistics.-p: shows detailed statistics of a specific partition or disk.
Combining vmstat with other tools
For a deeper analysis,vmstatis commonly used together withtop,
#! / bin / bashwhile true; do echo "$(date) -- -- CPU and MEM -- -- "vmstat 1 2 | tail -1 echo "$(date) ---- I / O of disk -- -- "iostat -x 1 2 | tail -5 sleep 5done
This type of combination allows to correlate CPU use peaks with increases in waiting for I / O or in the pagation, facilitating the identification of bottlenecks.
Good practices and advice
- Run
vmstatwith too low delay (e.g. 0.1 seconds) can generate unnecessary overload; values between 1 and 5 seconds are suitable for most monitoring scenarios. - Save output in a log file facilitates further analysis:
vmstat 5 > /var/log/vmstat_$(date +%F).log. - In virtualized environments, pay attention to the column
st (tiempo robado) ayuda a detectar cuando el hipervisor está limitando los recursos de la máquina invitada. - Use the option
-ain modern systems with much memory can provide a clearer view of the memory actually available for applications.
Conclusion
The commandvmstatis a light but powerful tool that provides a complete snapshot of memory status, CPU and I / O subsystem in Linux systems. Its simplicity of use, combined with the ability to customize output through options and scripts, makes it an indispensable ally for any administrator who seeks to maintain the performance and stability of their servers.


