Let say you have 2 GB of memory. When you are running an operating system, it may that your available memory is only 1,5 GB. Then you actively using a lot of applications. When the usage of the memory is full, you cannot load any more applications.
To solve this problem, operating system including Linux use a method named Virtual Memory. This method will search the area of memory which not used recently by any applications, then copy it into computer harddisk. This can give some free memory area and give you chance to run more applications.
To monitor these virtual memory activities, we can use vmstat tool.
What is vmstat
Vmstat is a tool that provide reporting virtual memory statistics. It covers system’s memory, swap and processors utilizations in real time.
How to run vmstat Command
Same with mpstat, vmstat is included inside sysstat package. If you don’t have it, please install sysstat package.
To run vmstat, just type vmstat in your console. Running vmstat without parameter will show you a default result of vmstat.
Let’s see how to read the information provided by vmstat :
Procs have r column and b column. r columns means the total number of processes that waiting access fot the processor. While b column means the total no processes in a sleep state. Below those columns, there are values. From the above screenshot, we have 2 processes that waiting access to the processor and 0 for sleep processes.
Memory have swpd, free, buff and cache columns. This information is the same with free -m command. The swpd column show you about how much memory has been swapped to a swap file or disk. The free column show you the unallocated memory available. Buff column means how much in use memory. And cache column show you about how much memory that can be swapped into swap file or disk if there are some application is needed by them.
Swap show us how much memory is sent or retrieved from the swap system. The si column tell us how much memory is moved from swap to real memory per second. The so column tell us how much memory that is moved from real memory to swap.
The io show us the amount of input and output activity per second in terms of blocks read and blocks written. The bi column tell us about the number of blocks received and the bo tell us about the number of blocks sent.
System show us the number of system operations per second. The in column tell us about the number system interrupts per second. The cs column tell us the number of context switches that the system makes in order to process all tasks.
CPU show us the use of CPU’s resources. The us column tell us how much time that processor spends on non-kernel processes. The sy column tell us about how much time that processor spends on kernel related tasks. The id column tell us about how long the processor are idle. The wa column tell us how much time that the processor has been waiting for I/O operations to complete before being able to continue processing tasks.
1) Using vmstat with delay
As a tool for statistics, the best way of using vmstat is using a delay. So you can capture the activity periodically. Let say we want to run vmstat with 5 second delay. Just type vmstat 5 on your console to do it.
The command will run every 5 seconds until you press Ctrl-C to stop it.
You can use count to limit how many times vmstat will running.
The above command will run vmstat with 5 seconds delay for 7 times.
2) Show active and inactive memory
To do it, you can add -a parameter to vmstat. Here’s a sample.
3) Show summarize disk statistics
vmstat can print your system disk statistics if you want. Use -D parameter to print it.
4) Show display unit
You can choose what unit character you want to print. Use -S followed by k (1000), K (1024), m (1000000), M (1048576) bytes. If you don’t choose the unit, vmstat will use K (1024).
5) Print detail statistics for a specific partition
To do it, you can use -p parameter followed by device name. Here’s an example of it.
vmstat is using these files to works.
If you feel your system is running out memory, before you add some physical memory, this tool can help you to determine the root cause of the problem. As usual, you can explore more detail about vmstat command by typing 'man vmstat' on your console.