Your Linux or macOS computer is using virtual memory. Discover how it’s affecting your system’s use of physical memory, CPU, and hard disk resources.
What Is Virtual Memory?
Your computer is fitted with a finite amount of physical memory called random access memory (RAM). This RAM needs to be managed by the kernel and shared between the operating system and whatever applications happen to be running. If these combined demands are asking for more memory than is physically installed in your computer, what can the kernel do?
Linux and Unix-like operating systems such as macOS can use space on your hard disk to help them manage memory demands. A reserved area of hard drive space called “swap space” can be used as though it were an extension of RAM. This is virtual memory.
The Linux kernel can write the contents of a block of memory into swap space, and free up that region of RAM for use by another process. The swapped out—also called “paged” out—memory can be retrieved from the swap space and restored to RAM when it is required.
Of course, the speed of access for paged out memory is slower than that of memory held in RAM. And that’s not the only trade-off. Whilst virtual memory does provide a way for Linux to manage its memory demands, using virtual memory places increased burdens elsewhere on the computer.
Your hard drive must perform more reads and writes. The kernel—and hence, the CPU—must do more work as it swaps memory out, swaps memory in, and keeps all the plates spinning to satisfy the memory needs of the different processes.
Linux provides a way for you to monitor all of this activity in the shape of the vmstat command, which reports on virtual memory statistics.
The vmstat Command
If you type vmstat as a command with no parameters, it will show you a set of values. These values are the averages for each of the statistics since your computer was last rebooted. These figures are not a snapshot of the values “right now.”