Your Linux or MacOS computer uses virtual memory. Learn how this affects the use of your system's physical memory, CPU, and disk resources.
What is Virtual Memory?
Your computer has a limited amount of physical memory, called memory. This RAM must be managed by the kernel and shared between the operating system and the applications currently running. If these combined requirements require more memory than physically installed on your computer, what can the kernel do?
Linux and Unix-like operating systems, such as macOS, can use space on your hard drive to manage memory requirements. A reserved area of the disk space called "swap space" can be used as a RAM extension. This is virtual memory.
The Linux kernel can write the contents of a memory block to swap space and free that RAM area for use by another process. Outsourced storage ̵
Of course, the paged memory access speed is slower than the random access memory RAM. And that is not the only compromise. While virtual memory provides Linux with the ability to manage its storage needs, using virtual memory increases the load on the computer elsewhere.
Your hard disk needs more reads and writes. The kernel – and thus the CPU – has to do more work, as it offloads storage, stores storage, and rotates all disks to cover the storage needs of different processes.
Linux provides a way to monitor all these activities in the form of command
vmstat which outputs statistics to virtual memory.
The vmstat Command
If you enter
vmstat as a command with no parameters, it displays a series of values. These values are the averages for each statistic since the last time you restarted your computer. These numbers are not a snapshot of the values "at the moment".
A short table of values is displayed.
There are columns with the headings Procs, Memory, Swap, IO, System and CPU. The last column (far right column) contains the data for the CPU.
Here is a list of the column data items.
- r : The number of executable processes. These are processes that have been started and are either running or waiting for their next scheduled outbreak of CPU cycles.
- b : The number of processes in uninterrupted hibernation. The process does not sleep, executes a blocking system call and can not be interrupted until the current action is completed. Typically, the process is a device driver waiting for a resource to become free. All interrupts in the queue for this process are processed when the process resumes normal activity.
- swpd : The amount of virtual memory used. In other words, how much memory has been swapped out.
- Free : The amount of free (currently unused) memory.
- Buffer : The amount of memory used as a buffer.
- Cache : The amount of memory used as a cache.
- si : The amount of virtual memory paged out of the swap space in .
- Thus, : amount of virtual memory was paged out to swap space.
- bi : Blocks received by a block device. The number of data blocks used to restore virtual memory to memory.
- bo : blocks sent to a block device. The number of data blocks used to offload the virtual memory from the RAM into the swap space.
- in : The number of interrupts per second, including the clock.
- cs : The number of context changes per second. A context switch occurs when the kernel switches from system mode processing to user mode processing.
These values are all percentages of total CPU time.
- us : Time spent executing non-kernel code. That is, how much time is spent processing user time and time-saving processing.
- sy : Time to execute the kernel code.
- id : Time for inactivity.
- wa : Waiting time for input or output.  st : Time stolen from a virtual machine. This is the time that a virtual machine must wait for the hypervisor to complete servicing other virtual machines before it can process that virtual machine again.
With a time interval
We can provide
vmstat [19459013haben] Regular updates of these numbers with a value for
delay . The value for
delay is given in seconds. To update the statistic every five seconds, we use the following command:
Every five seconds
vmstat is added another command line of data on the table. You must press Ctrl + C to stop this.
Using a count value
Using a too low
delay Will add weight to your system. If you need quick updates to diagnose a problem, it is recommended to use the value
count as well as the value
The value indicates How many updates must be done before the program exits and returns you to the command prompt. If you do not specify a value for
vmstat will be executed until it is stopped by Ctrl + C.
vmstat provides an update every five seconds - but only for four updates - use the following command:
vmstat 5 4
 After four updates
vmstat stops by itself.
-S key to specify that the storage and paging statistics are displayed in kilobytes or megabytes (unit character) option. This is followed by one of
M . These represent:
- k : 1000 bytes
- K : 1024 bytes
- m : 1000000 bytes
- M : 1048576 bytes
Statistics have Use the following command, which is updated every 10 seconds with the display of memory and paging statistics in megabytes:
vmstat 10 -SM
The memory and paging statistics are now displayed in megabytes. Note that option
-S does not affect I / O block statistics. These are always displayed in blocks.
Active and inactive memory
If you use the
-a (active) option The buff and cache memory Columns are replaced by the columns "inact" and "active". As you suspect, these indicate the size of inactive and active memory.
To display these two columns instead of the buff and cache columns, include the option
-a as shown:
vmstat 5 -a-SM
The inactive and active columns are affected by the -S option (unit character) . 
-f switch indicates the number of forks that have occurred since booting the computer.
In other words, it displays the number of tasks that have been started (and mostly closed since system startup). Any process started from the command line would increase that number. Each time a task or process creates or clones a new task, that number increases.
The display of the forks is not updated.
The kernel has its own memory management and memory management for the operating system and all applications.
As you can imagine, the kernel allocates memory to the kernel over and over again and gives the mapping to many different types of data objects it needs to process. To make this as efficient as possible, a system called plates is used. This is a form of caching.
Memory allocated, used, and unneeded for a particular type of kernel data object can be reused for another data object of the same type without removing and reassigning the memory allocation. Think of disks as pre-assigned, custom RAM segments for the kernel's own needs.
To view the statistics for the disks, use the option
-m (disks). You must use
sudo and be prompted to enter your password. Since the issue can be very long, we divert it by
sudo vmstat -m | less
The output contains five columns. These are:
- Cache : Name of the cache.
- num : The number of currently active objects in this cache.
- total : The total number of available objects in this cache cache.
- size : The size of each object in the cache.
- pages - The total number of memory pages to which (at least) one object is currently associated.
q to exit
Viewing Event Counters and Memory Statistics
Viewing a page with event counters and memory statistics Use
-s (statistics). Note that this is a lowercase letter "s".
Although the statistics reported are largely consistent with the information that composes the default
] vmstat is output, some of them are divided in more detail.
For example, standard output combines both nice and non-nice user CPU time in the us column. In the -s (statistics) display, these statistics are listed separately.
Viewing Disk Statistics
For a similar list of disk statistics, see the
-d (disk) option.
vmstat -d | less
Three columns are displayed for each volume: Reads, Writes, and IO.
IO is the far right column. Note that the sec column is measured in IO in seconds, but the time-based statistics in the read and write columns are measured in milliseconds.
This is what the columns mean:
- in total : The total number of volumes read.
- merged : The total number of clustered volumes reads.
- Sectors : The total number of scanned sectors.
- ms : Total number of milliseconds of time used to read data from disk.
writes  total : The total number of hard disk writes.
- cur: Number of current reads or writes to disk.
- sec: Time in seconds for read or write operations in progress.
Viewing a summary of disk statistics
-D [19umeineschnelleAnzeigederZusammenfassungsstatistikenfürIhreFestplattenaktivitätzuerhalten459013] (disk-sum) option. Note the uppercase letter "D".
The number of disks may be unusually high. The computer on which this article was researched is running Ubuntu. With Ubuntu, every time you install an application from a snap, it creates a
squashfs pseudo-file system attached to a / dev / loop device.
These device entries are annoyingly counted as disk devices by many Linux commands and utilities.
Displaying partition statistics
To display statistics about a particular partition, use the
-p (partition) and type The partition ID as a command line parameter.
Here the partition
sda1 is considered. The numeral 1 indicates that this is the first partition on device
sda which is the main hard drive for this computer.
vmstat -p sda1
19659006] The information returned shows the total number of disk reads and writes to and from this partition, as well as the number of sectors used in the Read and Read actions Writing hard drives are included.
A look under the hood
It is always good to know how to lift the hood and see what goes on underneath , Sometimes you will try to solve a problem, sometimes you are not interested because you want to know how your computer is ticking.
vmstat can provide you with a lot of useful information. Now you know how to access it and what it means. And be forewarned - if you need to roll up your sleeves and make a diagnosis, you know you have
vmstat on your side.