Swap: when RAM overflows onto disk
Video 042 of 140 in Virtualization Foundations, in the unit “Memory: RAM, addresses and virtual memory”. 3:57, with English and Hindi audio.
Swap is disk space that the kernel uses when RAM runs short. It lets a server keep working when programs together ask for more memory than the machine has free, but swap is much slower than RAM. This article explains why swap exists, how the kernel moves idle pages to it and brings them back, why too much swap makes a machine crawl, and how to check swap use on an Ubuntu server.
What you will learn
- Swap is disk space used when RAM runs short.
- The kernel moves idle pages there, and brings them back when a program needs them.
- Swap is a safety net, and it is much slower than RAM.
- Heavy swapping means thrashing, so watch the si and so columns in vmstat.
Why a server uses swap
A server has a fixed amount of RAM, and all the programs together may ask for more than that. But much of a program's memory sits idle for long stretches, because those pages are not needed at this moment. So the kernel, which is the core of the operating system, can save idle pages to swap, and free the RAM for work that is active now.
How the kernel moves pages to swap and back
- RAM fills up, because a program needs more memory.
- The kernel looks for pages that have not been used lately.
- It writes those pages to swap, and their RAM is freed for other work.
- If the program touches one of those pages again, a major fault happens, and the kernel reads the page back from swap.
A major fault is a memory request that has to wait for a disk read. That read is slow, because a disk is far slower than RAM, so a program that keeps doing it feels stuck. Swap can be a swap partition, which is a disk area set aside for it, or a swap file inside an ordinary file system.
Swap compared with RAM
RAM is fast, and its size is the memory the machine was built with. Swap is on disk, which is much slower. Its size is chosen by the administrator, within the space the disk has.
Swap is a safety net for short bursts. It is not extra memory that runs at RAM speed.
Why too much swap hurts a server
- Thrashing: pages keep moving between RAM and disk, and the machine spends its time moving data instead of working.
- Slow response: every touch of a swapped page waits for the disk.
You would notice slow logins and commands, a busy disk, and swap counts that keep rising.
How to check swap on Ubuntu
On Ubuntu, swapon with the show option lists the swap areas in use, with their sizes and how much of each is used.
swapon --showThe free command, with the h option, has a Swap line with total, used and free swap, in readable units.
free -hThe vmstat tool, run every five seconds three times, shows its si and so columns. These show how much memory moves in from swap and out to swap. On a healthy machine these columns stay low.
vmstat 5 3Try it yourself
You need an Ubuntu machine. You will see the swap areas in use, the Swap line of the memory summary, and the swap in and swap out columns of a short vmstat sample.
List the swap areas in use, with their sizes and how much of each is used.
swapon --showWhat you should see: Lists each swap area in use, with its size and how much is used.
Show the memory summary, with the Swap line in readable units.
free -hWhat you should see: The Swap line gives total, used and free swap, in readable units.
Sample memory activity three times, five seconds apart. Watch the si and so columns, which show memory moving in and out of swap.
vmstat 5 3What you should see: The si and so columns show how much memory moves in from swap and out to swap. On a healthy machine these stay low.
Terms
- Swap
- Disk space the kernel uses as overflow for memory pages when RAM is short.
- Major fault
- A memory request that has to wait for a disk read, such as reading a page back from swap.
- Thrashing
- The bad state in which pages keep moving between RAM and disk, and little real work gets done.
Check yourself
Why does the kernel move idle pages to swap?
To free RAM for active work, because much of a program's memory sits idle for long stretches.
Why does a program feel stuck when it touches a page that was moved to swap?
Reading the page back from disk is much slower than reading RAM, so the program has to wait.
What is thrashing?
The state in which pages keep moving between RAM and disk, so the machine spends its time moving data instead of working.
Previous: Page faults: the kernel's on-demand help. Next: The page cache: spare RAM that is not wasted.