Swap
Somewhere to put memory pages that are not being used, so RAM can go to pages that are, whether that place is a disk or a compressed region of RAM itself.
Swap is storage the kernel can move memory pages into when RAM is under pressure, freeing physical memory for pages in active use. With Virtual Memory, a page's location is invisible to the Process that owns it, so a page can be written out and brought back on its next access through a page fault, slower but otherwise transparent.
The common misreading is that swap is emergency memory for when RAM runs out. Its main job is different. Memory comes in two kinds: file-backed pages, which can always be dropped and re-read from their file, and anonymous pages (heap, stacks) with no file behind them. Without swap, anonymous pages can never be evicted, however cold. Under pressure the kernel can then only reclaim file cache, including the code of running programs, and ends up re-reading the same hot files from disk over and over. That is thrashing: the machine stays technically alive and becomes unusable, and on a desktop it is often worse than a clean OOM kill would be. A little swap lets the kernel push out memory nobody has touched in hours and keep the working set resident.
Compressed swap changes the economics. zram creates a block device in RAM that compresses whatever is written to it, commonly at two to four times. Swapping to it costs CPU time rather than disk I/O, which on a fast processor is cheap. zswap is a compressed cache in front of a disk swap device. Both rest on the observation that cold memory is highly compressible. Swap can also be rationed per cgroup, so one group can't fill the swap device for everyone.
Swap usage is a symptom that needs context. A large amount sitting in swap and rarely touched is healthy, since it is memory nothing needs. Swap traffic, pages moving in and out continuously, is the problem, because it means the working set no longer fits. On a long-running machine, swap and compressed swap fill gradually with the cold remains of programs that leaked or were never closed, which is a reason "it got slow after a week" so often goes away after a reboot. Pressure Stall Information measures the distinction directly, where raw usage figures can't.
See also6
Suspend and Hibernation
The power states that let a computer pause instead of shutting down, keeping memory powered for a fast resume or saving it to disk for a resume with no power at all, and a recurring source of laptop bugs.
Systems & Tooling9 connections
Dynamic Random-Access Memory
The main memory of nearly every computer, which stores each bit as charge in a tiny capacitor that leaks and must be refreshed many times a second, making it cheap and dense but slow next to the processor.
Systems & Tooling4 connections
Least Recently Used Cache
A fixed-capacity cache that evicts whichever entry has gone untouched the longest.
Computation & Algorithms13 connections
Resource Starvation
Failures caused by competition for a finite shared resource rather than by any defect.
Testing & Verification18 connections
Page Cache
The operating system's use of spare memory to hold recently read and written file data, which makes repeated disk access fast and makes "free memory" look alarmingly low.
Systems & Tooling11 connections
Load Average
The three numbers in uptime and top, what Linux actually counts in them, and why they are a rough smoke alarm rather than a measure of how busy a machine is.
Systems & Tooling4 connections
Linked from7
- Dynamic Random-Access MemorySystems & Tooling
The main memory of nearly every computer, which stores each bit as charge in a tiny capacitor that leaks and must be refreshed many times a second, making it cheap and dense but slow next to the processor.
- Load AverageSystems & Tooling
The three numbers in uptime and top, what Linux actually counts in them, and why they are a rough smoke alarm rather than a measure of how busy a machine is.
- Out-of-Memory KillerSystems & Tooling
The kernel's last resort when memory is exhausted, killing a process chosen by heuristic, and the userspace daemons that act earlier with better judgment.
- Page CacheSystems & Tooling
The operating system's use of spare memory to hold recently read and written file data, which makes repeated disk access fast and makes "free memory" look alarmingly low.
- Pressure Stall InformationSystems & Tooling
A kernel metric for how much time work spent waiting on CPU, memory, or I/O, which measures contention directly where utilization figures only hint at it.
- Suspend and HibernationSystems & Tooling
The power states that let a computer pause instead of shutting down, keeping memory powered for a fast resume or saving it to disk for a resume with no power at all, and a recurring source of laptop bugs.
- Virtual MemorySystems & Tooling
Giving every process its own private address space that the kernel maps onto physical memory page by page, on demand.