Swap Space
- In Turkish
- Swap Alanı
In short
Swap space is an area on disk that the operating system uses as overflow for RAM, moving rarely used memory pages there when physical memory runs low.
What is swap space?
Swap space is a dedicated partition or file on a disk that extends the memory an operating system can use. When RAM fills up, the kernel picks memory pages that haven't been used recently, writes them to swap, and frees that RAM for active work. If a program later touches a page that was swapped out, the kernel reads it back from disk before the program can continue.
On Linux, swap can be a partition or a regular file, and a setting called swappiness controls how eagerly the kernel swaps compared with dropping cached file data. Windows uses a page file named pagefile.sys, and macOS combines swap files with memory compression. Only memory that exists nowhere else, such as a program's heap and stack, needs swap; pages holding program code can simply be discarded and reread from the original file. Some systems also compress pages in RAM before resorting to disk, which is faster than swapping.
Swap is like renting a storage unit when your closet overflows: moving rarely used boxes there frees space at home, but fetching something takes a trip. If you keep shuttling the same boxes back and forth, you spend all day driving. In computing this is called thrashing, and it makes a system crawl because even a fast SSD is many times slower than RAM.
Swap space is often confused with virtual memory, but it is only one part of it; a system still uses virtual memory with swap turned off. Swap is not a substitute for enough RAM, and it doesn't fix a memory leak, it only delays the crash. Without swap, a Linux system that runs out of memory calls the out-of-memory (OOM) killer to terminate a process, and with swap it slows down first, which is why many servers and container hosts run with little or no swap to keep performance predictable.
Key takeaways
- Swap space is disk storage used as overflow when RAM is full.
- The kernel moves rarely used pages to swap and reads them back on demand.
- Heavy swapping, called thrashing, makes a system extremely slow.
- Swap is one part of virtual memory, not the same thing.
- Windows calls its swap area the page file.
Example
# Show RAM and swap usage
free -h
# List active swap areas
swapon --show
# Create and enable a 2 GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# How eagerly the kernel swaps (0-200, default 60)
cat /proc/sys/vm/swappinessReaders ask
How much swap space do I need?
There is no single rule. Desktops and laptops often use a few gigabytes, or at least as much as RAM if you want hibernation, while many servers use a small amount or none so that memory problems show up quickly instead of causing slowdowns.
Is swap bad for SSDs?
Occasional swapping is fine for modern SSDs, which are built to handle many writes. Constant heavy swapping is a problem mainly because it slows the system down, and it signals that the machine needs more RAM.
What is thrashing?
Thrashing is when the system spends most of its time moving pages between RAM and swap instead of doing useful work. It happens when the active programs need much more memory than the machine has.
See also
- Virtual MemoryOperating Systems, p. 37Virtual memory is an operating system technique that gives each process its own private address space and maps it to physical RAM or disk behind the scenes.
- PagingOperating Systems, p. 22Paging is a memory management scheme that splits memory into fixed-size pages and uses page tables to map each process's virtual pages to physical RAM.
- Memory LeakOperating Systems, p. 19A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.
- KernelOperating Systems, p. 17A kernel is the core part of an operating system that manages the CPU, memory, and hardware devices and controls how programs get access to those resources.
- File SystemOperating Systems, p. 12A file system is the part of an operating system that organizes data on a storage device into files and folders and tracks where each piece is stored.
- ProcessOperating Systems, p. 23A process is a running instance of a program, with its own memory space, resources, and at least one thread of execution managed by the operating system.
Spotted a mistake or something missing on this page?Suggest an edit