How to use the command vmstat (with examples)
- Linux
- December 25, 2023
The vmstat
command is a powerful tool that provides information about processes, memory, paging, block IO, traps, disks, and CPU activity on a Linux system. It is a handy utility for system administrators, developers, and anyone interested in monitoring and troubleshooting their system’s performance.
Use case 1: Display virtual memory statistics
Code:
vmstat
Motivation:
Displaying virtual memory statistics can help you understand how the system is utilizing its memory resources. By analyzing the output of vmstat
, you can identify any memory-related issues or bottlenecks and take appropriate actions to optimize memory usage.
Explanation:
Running the vmstat
command without any arguments will display a summary of various statistics related to virtual memory. These include:
- procs: Information about processes such as the number of running processes, waiting processes, and process creation rate.
- memory: Information about memory usage, including the amount of free memory, memory used by buffers and caches, and swap space utilization.
- swap: Information about swap memory usage, including the amount of swap space used and the amount of data being paged in and out.
- io: Information about block I/O activity, including the number of blocks read and written per second.
- system: Information about CPU and interrupts, such as the number of context switches and interrupts per second.
- cpu: Information about CPU usage, including the amount of time spent in user, system, idle, and wait states.
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 949456 19208 141444 0 0 0 1 4 5 0 0 100 0 0
In this example output, we can see various statistics such as the number of running and waiting processes, the amount of free memory, the amount of data being paged in and out of swap space, and CPU usage breakdown.
Use case 2: Display reports every 2 seconds for 5 times
Code:
vmstat 2 5
Motivation:
Monitoring system performance in real-time can be crucial when troubleshooting performance issues or analyzing system behavior under different scenarios. Using the -n
option with the vmstat
command allows you to continuously collect statistics at regular intervals, providing an ongoing snapshot of your system’s performance.
Explanation:
By specifying the interval and count values as arguments to the vmstat
command, you can control the frequency and duration of the reports.
2
: This specifies the interval in seconds between each report.5
: This specifies the number of reports to display.
In this example, the vmstat
command will execute five times, with an interval of two seconds between each report.
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
0 0 0 949456 19208 141444 0 0 0 1 4 5 0 0 100 0 0
0 0 0 949456 19208 141444 0 0 0 0 105 198 0 1 99 0 0
0 0 0 949456 19208 141444 0 0 0 0 100 200 0 0 100 0 0
0 0 0 949456 19208 141444 0 0 0 0 100 199 0 0 100 0 0
0 0 0 949456 19208 141444 0 0 0 0 100 200 0 0 100 0 0
In this example output, we can see the virtual memory statistics reported every two seconds for a total of five times. Each report provides an updated snapshot of the system’s performance, allowing you to monitor changes and identify any trends or anomalies.