Your laptop has no swap. Here is what happens when memory runs out.
A common setup, especially on fast laptops with plenty of RAM: no swap at all, because "swap is slow" or because the installer did not create one. It works right up until the first memory spike, and then it fails in the worst possible way.
What the kernel does when memory runs out
Memory in use falls into two kinds. File-backed pages are copies of things that exist on disk: program code, libraries, the page cache. The kernel can drop them at any time and re-read them later. Anonymous pages are everything else: your browser tabs, the heap of whatever is running, the contents of a spreadsheet. They exist only in RAM, and the only place they can go when RAM is short is swap.
With no swap, anonymous pages have nowhere to go. So as memory fills, the kernel does the only thing it can: it evicts file-backed pages instead, including the code of the programs that are running. Those programs immediately need that code again, so it is read back from disk, evicted again, read again. This is thrashing. The machine is not crashed, but the mouse stutters, windows stop repainting, and the disk light stays on. It can last seconds or many minutes.
Eventually the kernel gives up and invokes the out-of-memory killer, which picks one process, usually the largest, and terminates it with no warning and no chance to save. On a desktop that is typically the browser; on a home server it is the database or the container you most needed.
That is the whole problem with no swap: the failure is not slow, it is abrupt, and it happens at the moment of peak demand.
What swap changes
With swap, the kernel has a second option. Anonymous pages that have not been touched for a while (the tab you opened yesterday, a service's start-up buffers) can be written out to make room. The active working set stays in RAM. Under a real spike the machine still slows down, but it slows down instead of killing things, and it recovers when the spike passes.
Two objections come up every time:
- "I have 16 GB, I don't need swap." The amount of RAM changes how often you hit the limit, not what happens when you do. The ThinkPad this guide was written on had 16 GB, ran a home server, and still hung the moment something briefly needed a little more.
- "Swap wears out the SSD." A modern SSD is rated for hundreds of terabytes of writes; occasional swapping is a rounding error. And the first tier of swap in the setup below does not touch the disk at all.
The two-tier setup: zram first, a swapfile behind it
zram is a compressed block device in RAM used as swap. Pages swapped to it are compressed (commonly 2–3× with zstd or lz4) and stay in memory, so "swapping" to zram costs a little CPU and no disk I/O. It is the cheapest possible cushion and is what Fedora enables by default.
A swapfile on the SSD is the second tier: slower, but it is where pages go once zram is full, and it is what stops hard kills during a sustained spike. Giving zram a higher priority than the file means the kernel fills zram first.
zram
# Debian / Ubuntu
sudo apt install zram-tools
sudoedit /etc/default/zramswap # e.g. ALGO=zstd PERCENT=50 PRIORITY=100
sudo systemctl restart zramswap
# Fedora (already on by default; config lives here)
sudoedit /etc/systemd/zram-generator.conf
# [zram0]
# zram-size = min(ram / 2, 4096)
# Arch
sudo pacman -S zram-generator # same config file as Fedora
A zram device of half your RAM is a sensible default; it only uses memory as pages are actually compressed into it.
Swapfile
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon --priority 5 /swapfile
echo '/swapfile none swap sw,pri=5 0 0' | sudo tee -a /etc/fstab
On btrfs, fallocate is not enough because the file must be created without copy-on-write; use sudo btrfs filesystem mkswapfile --size 8g /swapfile (btrfs-progs 6.1 or newer) instead of the first three lines. On ext4 and XFS the commands above are fine.
Check the result:
swapon --show
# NAME TYPE SIZE USED PRIO
# /dev/zram0 partition 4G 0B 100
# /swapfile file 8G 0B 5
That is the layout on the ThinkPad now: 4 GB of zram at priority 100 in front of an 8 GB file at priority 5. Innards reports it as "Swap is configured: 12.0 GiB of swap is available (8.0 GiB (File, prio 5), 4.0 GiB (Zram, prio 100))."
How much, and a few settings
- Size. zram at half of RAM; a swapfile of 4–8 GB is enough for a cushion on a machine with 8–32 GB. The old "twice your RAM" rule dates from when RAM was small.
- Hibernation needs a swap partition or file at least as large as the memory you actually use, and cannot use zram. If you hibernate, size the file with that in mind.
vm.swappiness(default 60) sets how eagerly the kernel swaps anonymous pages rather than dropping cache. With zram in front, a higher value is reasonable since swapping is cheap; the kernel accepts up to 200 since 5.8. It is a tuning knob, not a requirement.- Userspace OOM daemons. Ubuntu (since 22.04) and Fedora ship
systemd-oomd, which watches memory pressure and kills a whole application group before the kernel's killer has to act. It is a better-aimed kill, but it is still a kill; swap is what avoids it.
Windows and macOS
Both manage this for you, and both let you break it. On Windows, keep "Automatically manage paging file size for all drives" ticked (System → Advanced system settings → Performance → Virtual memory); disabling the pagefile to "save space" is the same failure mode as no swap on Linux. macOS creates swap files in /private/var/vm on its own and shows the state as the memory pressure graph in Activity Monitor; there is no supported way to turn it off, and no reason to.