How to use Vmstat command in Linux explained with examples

Thanks to the potential of the Linux terminal, we can enjoy tools that allow us to keep an eye on the performance of the computer. This time we will talk about the vmstat command and you will also learn how to use it with some examples.

In a nutshell, the vmstat command provides information about the computer’s virtual memory.

What is virtual memory?

As virtual memory, we should not understand it as an additional module that our computer has but as a technique of computer memory management, whose use lies in the joint use of the main memory of the system and our storage unit, such as a hard disk.

So it is nothing more than a trick used by the system to use part of our storage as an additional memory module. Something like the Linux swap.

As this space is used, it can have an impact on the speed of the computer because the RAM will always be faster than other types of storage.

So, thanks to the incredible potential of the Linux terminal, we can monitor it quickly and easily, and all thanks to the vmstat command.

The vmstat command

The vmstat command is used in UNIX operating systems, it provides data about virtual memory, gives information about processes, memory, paging, block I/O, traps, and CPU activity.

The first report produced gives averages since the last reboot. Additional reports give information on a sampling period of length delay. The process and memory reports are instantaneous in either case.

The basic syntax of the vmstat command is as follows

vmstat [options] [delay [count]]]

By default, the command comes to give a single report but we can modify it with the options and by setting a refreshing time.

Using the vmstat commmand

If you run the command without any options or parameters you will get an output screen similar to this one

vmstat

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 114432 212660  76392 650036    0    4    60    97  579  155 16  6 77  2  0

Where you will immediately notice that it is divided into some sections along with some values.

In the procs section are the system processes and these two indicators

  • r: The number of runnable processes (running or waiting for run time).
  • b: The number of processes blocked waiting for I/O to complete.

Then, we have the Memory section that examines the virtual memory itself:

  • swpd: the amount of swap memory used.
  • free: the amount of idle memory.
  • buff: the amount of memory used as buffers.
  • cache: the amount of memory used as cache.

As you can see, these are simple values and information.

Follow the Swap section:

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

Immediately follows the Input / Output section:

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

Next is the System section:

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

Finally, there is the CPU section:

  • us: Time spent running non-kernel code. (user time, including nice time)
  • sy: Time spent running kernel code. (system time)
  • id: Time spent idle.
  • wa: Time spent waiting for IO.
  • st: Time stolen from a virtual machine.

Some examples

Although the vmstat command is so easy to use, it is possible to get a lot out of it, so here are some examples.

Adding a time interval

It is possible to obtain values and results by adding a refreshing time that we can define. By default, the time is expressed in seconds.

For example, to get reports every 3 seconds you can run

vmstat 3

When you run the command with an interval time you will get infinite reports. To stop the command press the CTRL + C keys.

Interval time with a limit

But you can change this behavior and add another parameter that will print a defined number of reports.

vmstat 3 2

In this case, two reports will be printed at an interval of 3 seconds.

Making the report more readable

By default, the values shown by vmstat are expressed in blocks. You can change this to Mb or Kb by using the -S option.

To get expressions in Kb, use after the -S option the letter K or k.

vmstat 3 2 -S K

And then you will get the output on the screen expressed in Kb.

In the case of Mb, you can use the letters M and m

vmstat 3 2 -S M

And this way we can easily understand even more the generated reports.

Conclusion

A terminal is an incredible tool and in Unix, there are commands for everything. In this case, we have presented you with a command to monitor virtual memory usage. This kind of command helps us not to miss anything about our computer. Sysadmins appreciate this kind of command that does not consume resources and provides useful information.

More info: vmstat documentation

Scroll to Top