vmstat Command in Linux with Examples

vmstat – Virtual Memory Statistics. The vmstat command in Linux is utilized to display virtual memory statistics. It reports details about processes, memory, paging, block IO, traps, and CPU activity. This command proves beneficial for recognizing potential performance bottlenecks and diagnosing system-related issues.

Syntax:
The syntax for using vmstat command in Linux is as follows:

vmstat [options] [delay [count]]
  • vmstat: Command used to view virtual memory statistics.
  • [options]: Optional flags that modify the behavior of the vmstat command.
  • [delay]: The delay between updates in seconds. If specified without count, vmstat will continuously display statistics with the specified delay.
  • [count]: The number of times the statistics will be displayed before the command exits.

Usage Examples:

vmstat [-a] [-n] [-t] [-S unit] [delay [ count]]
vmstat [-s] [-n] [-S unit]
vmstat [-m] [-n] [delay [ count]]
vmstat [-d] [-n] [delay [ count]]
vmstat [-p disk partition] [-n] [delay [ count]]
vmstat [-f]
vmstat [-V]

Options:

  • -a: Enables the display of active/inactive memory information for kernels 2.5.41 or later.
  • -f: Displays the total number of forks since boot, including fork, vfork, and clone system calls. Each process is represented by one or more tasks, depending on thread usage. This display does not repeat.
  • -t: Adds timestamps to the output.
  • -m: Displays slabinfo information.
  • -n: Suppresses the repeating header and only displays it once.
  • -s: Displays a table of various event counters and memory statistics. This display does not repeat.
  • delay: Specifies the delay in seconds between updates. If no delay is specified, only one report is printed with the average values since boot.
  • count: Specifies the number of updates. If no count is specified and delay is defined, count defaults to infinity.
  • -d: Reports disk statistics (requires kernel 2.5.70 or later).
  • -w: Enlarges field width for displaying large memory sizes.
  • -p: Followed by a partition name, displays detailed statistics for that partition (requires kernel 2.5.70 or later).
  • -S: Followed by k, K, m, or M, switches the output units between 1000, 1024, 1000000, or 1048576 bytes.
  • -V: Displays version information.

vmstat Command Examples:

Example 1: Simple example along-with field descriptions

advertisement
advertisement

The vmstat command provides valuable insights into system performance, offering detailed statistics about various aspects of the system, such as memory, swap, I/O, CPU, and more.

$ vmstat
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 305416 260688  29160 2356920   2    2     4     1    0  0  6  1 92  2  0

Field Description For Vm Mode

Procs

  • r: The number of processes waiting for run time.
  • b: The number of processes in uninterruptible sleep.

Memory

  • swpd: the amount of virtual memory used.
  • free: the amount of idle memory.
  • buff: the amount of memory used as buffers.
  • cache: the amount of memory used as cache.
  • inact: the amount of inactive memory. (-a option)
  • active: the amount of active memory. (-a option)

Swap

  • si: Amount of memory swapped in from disk (/s).
  • so: Amount of memory swapped to disk (/s).

IO

  • bi: Blocks received from a block device (blocks/s).
  • bo: Blocks sent to a block device (blocks/s).

System

advertisement
  • in: The number of interrupts per second, including the clock.
  • cs: The number of context switches per second.

CPU

These are percentages of total CPU time.

  • us: Time spent running non-kernel code. (user time, including nice time)
  • sy: Time spent running kernel code. (system time)
  • id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
  • wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
  • st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.

Example 2: To execute every 2 seconds for 10 times

Executing the vmstat command with a specific interval and count provides an automated way to monitor system statistics:

$ vmstat 2 10
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 537144 182736 6789320   0    0     0     0    1  1  0  0 100  0  0
 0  0      0 537004 182736 6789320   0    0     0     0   50 32  0  0 100  0  0
..

This command will run vmstat every 2 seconds for 10 times, providing periodic updates on system statistics without the need for manual interruption.

advertisement

Example 3: Grep Memory Overview Using vmstat

The following command combines vmstat, -s, and grep to extract an overview of memory usage:

$ vmstat -s -S M | grep mem

This command extracts and displays essential memory statistics in megabytes.

5965 M total memory
5136 M used memory
3580 M active memory
1176 M inactive memory
829 M free memory
474 M buffer memory

Example 4: Display active and inactive memory

The -a option with vmstat displays active and inactive memory information:

$ vmstat -a
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 0  0 305416 253820 1052680 2688928    2    2     4     1    0    0  6  1 92  2  0

This command showcases the active and inactive memory along with other system statistics.

Example 5: Display number of forks since last boot

Use the -f option to display the count of forks (including fork, vfork, and clone system calls) since the last boot:

$ vmstat -f
     81651975 forks

This command provides the total count of fork system calls made by the system since the last boot.

Example 6: Display timestamp

When monitoring memory usage repeatedly with vmstat, including a timestamp along with each line item can be beneficial. Using the -t option enables displaying the timestamp, as demonstrated below:

$ vmstat -t 1 100
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 3608728 148368 3898200    0    0     0     0    1    1  0  0 100  0  0     2011-07-09 21:16:28 PDT
 0  0      0 3608728 148368 3898200    0    0     0     0   60   15  0  0 100  0  0     2011-07-09 21:16:29 PDT
 0  0      0 3608712 148368 3898200    0    0     0     0   32   28  0  0 100  0  0     2011-07-09 21:16:30 PDT

Example 7: Display slab info

To showcase slabinfo details, utilize the -m option with vmstat:

$ vmstat -m
Cache                       Num  Total   Size  Pages
fib6_nodes                    5    113     32    113
ip6_dst_cache                 4     15    256     15
ndisc_cache                   1     15    256     15
RAWv6                         7     10    768      5
UDPv6                         0      0    640      6
tw_sock_TCPv6                 0      0    128     30
...

Example 8: Display statistics in a table format

Instead of the default record format, the -s option allows displaying the vmstat output in a table format:

$ vmstat -s
      4149928  total memory
      3864824  used memory
      2606664  active memory
      1098180  inactive memory
       285104  free memory
        19264  buffer memory
      2326692  swap cache
      4192956  total swap
       274872  used swap
      3918084  free swap
   1032454000 non-nice user cpu ticks
        14568 nice user cpu ticks
     89482270 system cpu ticks
  16674327143 idle cpu ticks
    368965706 IO-wait cpu ticks
      1180468 IRQ cpu ticks
..

Example 9: Display disk statistics

To view disk statistics, utilize the -d option with vmstat. This provides information regarding disk reads, writes, and I/O:

$ vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
sda   153189971 69093708 2719150864 737822879 329617713 157559204 3965687592 4068577985      0 1102243
sdb   501426305 97099356 2345472425 731613156 419220973 533565961 2661869460 1825174087      0 1510434
sdc   884213459 22078974 513390701 452540172 127474901 8993357 2411187300 2133226954      0 1569758

Example 10: Changing width of output

$ vmstat 1 3
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 3608688 148368 3898204    0    0     0     0    1    1  0  0 100  0  0
 0  0      0 3608804 148368 3898204    0    0     0     0   72   30  0  0 100  0  0
 0  0      0 3608804 148368 3898204    0    0     0     0   60   27  0  0 100  0  0

Use option -w to increase the width of the output columns as shown below. This give better readability.

$ vmstat -w 1 3
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    3608712     148368    3898204    0    0     0     0    1    1   0  0 100  0  0
 0  0          0    3608712     148368    3898204    0    0     0     0   93   23   0  0 100  0  0
 0  0          0    3608696     148368    3898204    0    0     0     0   35   34   0  0 100  0  0

Example 11: Display statistics for a partition

To view disk I/O statistics for a specific disk partition, use the -p option followed by the partition name:

$ vmstat -p sdb1
sdb1          reads   read sectors  writes    requested writes
           501423248 2345417917  419221612 2661885948

Example 12: Display in MB

To display memory information in megabytes instead of kilobytes, use the -S m option:

$ vmstat -S m
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    281    288     19   2386    0    0     4     1    0    0  6  1 92  2  0

These options enhance the vmstat command’s capabilities, providing more specific and formatted insights into various system statistics.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.