How to use the vmstat command on Linux

Suppose your Linux or macOS computer is using virtual memory. Discover how it affects the system's physical memory usage, CPU and resource usage.

Suppose your Linux or macOS computer is using virtual memory. Discover how it affects the system's physical memory usage, CPU and resource usage.

Learn about the vmstat command in Linux

  1. What is virtual memory?
  2. Vmstat command
    1. Procs
    2. Memory
    3. Swap
    4. IO
    5. System
    6. CPU

  3. Use a period of time
  4. Use the count value
  5. Change units
  6. Memory is active and inactive
  7. Fork
  8. Slabinfo display
  9. Displays event counters and memory statistics
  10. Show drive statistics
    1. Reads
    2. Writes
    3. IO
  11. Show summary drive statistics
  12. Show partition statistics

What is virtual memory?

The computer is equipped with a finite amount of physical memory, called Random Access Memory (RAM). This RAM needs to be managed by the kernel, shared between the operating system and any running applications. If these combined needs require more memory than the physical installation in the computer, what can the kernel do?

Linux and Unix-like operating systems like macOS can use the space on the hard drive to help them manage memory needs. A dedicated area for hard drive space called 'swap space', can be used as an extension of RAM. This is virtual memory.

The Linux kernel can write the contents of a memory block into this swap space and release that RAM to use for another process. Memory can be removed from the swap space and restored to RAM when necessary called paged out.

Of course, access speed for paged out memory is slower than memory stored in RAM. And that is not the only tradeoff. While virtual memory provides Linux with a way to manage memory needs, the use of virtual memory will increase the burden on other parts of the computer.

Your hard drive must read and write more. The kernel and CPU must do more when swap in (move data from swap space to main memory) and swap out (move main memory contents to disk swap, when space is available). main memory is full) memory, as well as keeping all disks spinning to meet the memory needs of many different processes.

Linux provides a way for you to monitor all of these activities as vmstat commands, reporting virtual memory statistics.

Vmstat command

If you type the vmstat command without parameters, it will show you a set of values. These values ​​are the average for each statistic, since the computer was last restarted. These figures are not current values.

 vmstat 

How to use the vmstat command on Linux Picture 1How to use the vmstat command on Linux Picture 1

A short value table is displayed.

How to use the vmstat command on Linux Picture 2How to use the vmstat command on Linux Picture 2

There are Procs, Memory, Swap, IO, System and CPU columns. The last column (rightmost column) contains CPU-related data.

How to use the vmstat command on Linux Picture 3How to use the vmstat command on Linux Picture 3

Below is a list of data items in each column.

Procs

  1. r: Number of processes that can be run. These are processes that are already running or waiting for a new CPU cycle.
  2. b: Number of processes in continuous sleep state. In fact, these processes are not 'sleeping', but they are only blocking system calls (when the program calls a function or service that is in the kernel of the operating system) and cannot be interrupted until it executes. The current dynamic is completed. Typically, these processes occur when the device driver is waiting for some 'free' resources. Any disruption to these processes while in the queue is processed when the process continues to operate as usual.

Memory

  1. swpd: Virtual memory capacity used. In other words, this is the amount of memory that has been swapped.
  2. free: Idle memory capacity (currently not in use).
  3. buff: The amount of memory used as a buffer.
  4. cache: The amount of memory used to cache.

Swap

  1. si: The amount of virtual memory is swapped in from the swap space.
  2. so: The amount of virtual memory is swapped out into the swap space.

IO

  1. Ball: Blocks received from a block device. The number of data blocks used to exchange virtual memory back to RAM.
  2. bo: Blocks sent to a block device. The number of data blocks used to exchange virtual memory out of RAM and into swap space.

System

  1. Print: The number of interruptions per second, including the clock.
  2. cs: Number of context switches per second. The switch context is when the kernel changes from processing system mode to user mode processing.

CPU

These values ​​are all percentage of total CPU time.

  1. us: Time to run code is not kernel.
  2. sy: Runtime of kernel code.
  3. id: Idle time.
  4. wa: Time to wait for input or output.
  5. st: Time obtained from a virtual machine. This is the time a virtual machine must wait for the hypervisor to complete serving other virtual machines before it can go back and process the virtual machine.

Use a period of time

You can ask vmstat to provide regular updates for these metrics by using the delay value . The delay value is provided in seconds. To update statistics every 5 seconds, use the following command:

 vmstat 5 

How to use the vmstat command on Linux Picture 4How to use the vmstat command on Linux Picture 4

Every 5 seconds vmstat will add another row of data to the table. You need to press Ctrl + C to stop this.

How to use the vmstat command on Linux Picture 5How to use the vmstat command on Linux Picture 5

Use the count value

Using a delay value too low will put more pressure on the system. If you need to have quick updates to try to diagnose the problem, you should use the count value as well as the delay value .

The count value for vmstat indicates how many updates it needs to perform before exiting and returning you to the Command Prompt. If you do not provide the count value , vmstat will run until it is stopped by the Ctrl + C key combination.

Let vmstat provide an update every 5 seconds, but in just 4 updates, use the following command:

 vmstat 5 4 

How to use the vmstat command on Linux Picture 6How to use the vmstat command on Linux Picture 6

After 4 updates, vmstat will stop.

How to use the vmstat command on Linux Picture 7How to use the vmstat command on Linux Picture 7

Change units

You can choose to display memory statistics and swap in kilobytes or megabytes using the -S (unit character) option. This option must be followed by one of the units k, K, m or M. They represent:

  1. k: 1000 bytes
  2. K: 1024 bytes
  3. m: 1000000 bytes
  4. M: 1048576 bytes

To update memory statistics and swaps every 10 seconds, displayed in megabytes, use the following command:

 vmstat 10 -SM 

How to use the vmstat command on Linux Picture 8How to use the vmstat command on Linux Picture 8

Memory and swap statistics are currently displayed in megabytes. Note that the -S option does not affect IO block statistics . They are always displayed in blocks.

How to use the vmstat command on Linux Picture 9How to use the vmstat command on Linux Picture 9

Memory is active and inactive

If you use the -a (active) option, the cache and cache columns are replaced with 'inact' and 'active' columns . As you might have guessed, these columns show the amount of memory inactive and inactive.

To see these two columns instead of buff and cache columns , include the -a option , as shown:

 vmstat 5 -a -SM 

How to use the vmstat command on Linux Picture 10How to use the vmstat command on Linux Picture 10

'Inact' and 'active' columns are affected by the -S (unit character) option.

How to use the vmstat command on Linux Picture 11How to use the vmstat command on Linux Picture 11

Fork

Switch -f displays the number of fork that has occurred since the computer was started. (Fork is an activity where a process creates a copy of itself).

In other words, this indicates the number of tasks that were started (and most of them closed) since the system was started. Each process started from the command line will increase this number. Every time a task or process generates or duplicates a new task, this number will increase.

 vmstat -f 

How to use the vmstat command on Linux Picture 12How to use the vmstat command on Linux Picture 12

The display of fork does not update.

Slabinfo display

Kernel has its own memory management feature to manage the operating system and all applications.

As you can imagine, the kernel is allocating and releasing memory multiple times for many different types of data objects it must handle. To make this as effective as possible, it uses a system called slab. This is a form of cache record.

Memory is allocated, used and no longer needed for a specific type of kernel data object, which can be reused for another data object of the same type without the need for memory to be released and allocated. again. Imagine that the slab has been pre-allocated and measures the RAM segments for the kernel's specific needs.

To see statistics for slab, use the -m (slab) option. You need to use sudo and will be prompted to enter a password. Since the output can be quite long, you should use the less option .

 sudo vmstat -m | less 

How to use the vmstat command on Linux Picture 13How to use the vmstat command on Linux Picture 13

The output has 5 columns, including:

  1. Cache: The name of the cache.
  2. num: Number of objects currently active in this cache.
  3. total: The total number of objects available in this cache.
  4. size: Size of each object in the cache.
  5. pages: The total number of memory pages that have (at least) an object currently associated with this cache.

How to use the vmstat command on Linux Picture 14How to use the vmstat command on Linux Picture 14

To exit less mode , press q .

Displays event counters and memory statistics

To display a page of event counters and memory statistics, use the -s (stats) option. Note that, the letter s is lower

 vmstat -s 

How to use the vmstat command on Linux Picture 15How to use the vmstat command on Linux Picture 15

Although the statistics are reported in much the same way as the information generated by the default vmstat command, some of the data is divided in more detail.

For example, the default output combines both nice and non-nice user CPU time into the us column. The -s (stats) option lists these statistics separately.

How to use the vmstat command on Linux Picture 16How to use the vmstat command on Linux Picture 16

Show drive statistics

You can get a similar drive statistics list using the -d (disk) option.

 vmstat -d | less 

How to use the vmstat command on Linux Picture 17How to use the vmstat command on Linux Picture 17

For each drive, there are 3 columns displayed, which are Reads, Writes and IO.

How to use the vmstat command on Linux Picture 18How to use the vmstat command on Linux Picture 18

IO is the rightmost column. Note that the sec column in IO is measured in seconds but time-based statistics in Reads and Writes columns are measured in milliseconds.

How to use the vmstat command on Linux Picture 19How to use the vmstat command on Linux Picture 19

This is the meaning of the columns:

Reads

  1. total: The total number of read drives.
  2. merged: The total number of group readings.
  3. sectors: The total number of sectors read.
  4. ms: Total time in milliseconds used to read data from the drive.

Writes

  1. total: The total number of drive writes.
  2. merged: The total number of records grouped.
  3. sectors: The total number of sectors recorded.
  4. ms = Total time, in milliseconds, was used to write data to the drive.

IO

  1. cur: The number of reads or writes of the current drive.
  2. sec: Time in seconds for any reading or writing being performed.

Show summary drive statistics

To quickly display summary statistics for disk activity, use the -D (disk-sum) option. Note that the letter D is capitalized.

 vmstat -D 

How to use the vmstat command on Linux Picture 20How to use the vmstat command on Linux Picture 20

The number of drives may look unusually high. The computer used as an example in this article is running Ubuntu. With Ubuntu, every time you install a Snap application, a file system created with the squashfs pseudo-filesystem will be attached to the device / dev / loop.

The annoying thing is that these device items are counted as hard drive devices by many Linux commands and utilities.

How to use the vmstat command on Linux Picture 21How to use the vmstat command on Linux Picture 21

Show partition statistics

To see statistics related to a specific partition, use the -p (partition) option and provide the partition identifier as the command line parameter.

Here we will look at the partition sda1. The first digit indicates that this is the first partition on the sda device , this is the main hard drive for this computer.

 vmstat -p sda1 

How to use the vmstat command on Linux Picture 22How to use the vmstat command on Linux Picture 22

The return information shows the total number of reads, writes the drive to and from that partition, as well as the number of sectors included in the read and write operations.

How to use the vmstat command on Linux Picture 23How to use the vmstat command on Linux Picture 23

In-depth understanding of a problem is always encouraged. Sometimes you will try to solve the problem or not care about it anymore (because you just want to know how your computer works).

The vmstat command can give you a lot of useful information. Now, you know how to access this command and its meaning. You need to take some time to learn more and make some diagnoses, to really capture vmstat.

Good luck!

4 ★ | 6 Vote