Swap on Linux: partition vs swapfile vs zram, and how much
Swap is the overflow space Linux uses when RAM fills up, and the installer offers a few ways to set it up. A beginner-friendly look at swap partitions, swap files, and zram, with sizing guidance.
level:Beginner
During an Arch install there is an optional step where you set up swap, and it is genuinely optional: a modern desktop with a comfortable amount of RAM will run fine with no swap at all, so if you want to skip the decision, you can. But swap is cheap insurance and it unlocks one feature people miss when it is gone, so the better default for most people is to set up a small amount. The question is which kind of swap. There are three forms, they are easy to mix up, and the right answer for a normal desktop is not the classic one most old tutorials reach for. Here is what swap is, the three ways to provide it, and how much you actually need.
What swap is for
Your programs live in RAM, which is fast but finite. Swap is overflow space, normally on your disk, that the kernel can use when RAM gets tight. This is not a Linux invention: Windows does exactly the same thing with its pagefile, and macOS quietly grows dynamic swap files as needed. All three have decided that when physical memory runs low, the sane move is to spill the least-used pages somewhere slower rather than crash. Linux just makes the form of that overflow something you choose at install time.
When memory fills up, the kernel takes pages of memory that have not been used recently and writes them out to swap, freeing that RAM for whatever needs it now. If a swapped-out page is needed again later, the kernel reads it back in. Disk is far slower than RAM, so swap is a pressure valve, not extra RAM, but that pressure valve is the difference between a sluggish machine and one that hard-crashes or kills your programs when memory runs out.
RAM fills, idle pages move to swap, and they come back on demand.
flowchart LR
A["RAM fills up"] --> B["Kernel finds<br/>idle pages"]
B --> C["Pages written to swap<br/><i>disk or zram</i>"]
C --> D["RAM freed for<br/>what needs it now"]
D -->|"page needed again"| E["Kernel reads it<br/>back into RAM"]
The Arch Wiki Swap page and Wikipedia's article on memory paging cover the mechanism in full.
There is a second job that catches people out: hibernation. Hibernating, as opposed to ordinary suspend, writes the entire contents of RAM to disk and powers the machine fully off, then restores it on the next boot. It writes that image into swap, which means hibernation only works if you have disk-backed swap large enough to hold what is in memory. If you want hibernation, swap stops being optional and its size starts to matter. More on that below.
Swap partition: the classic form
A swap partition is a dedicated slice of the disk, formatted not as a filesystem but as swap space with mkswap. This is the traditional way, and for a long time it was the only way that was reliably fast. On a GPT disk it has its own partition type, and the kernel uses it directly without going through a filesystem.
Its downside is rigidity: you size it when you partition the disk, and changing it later means repartitioning, which is awkward on a system that is already installed and full of data. There is nothing wrong with a swap partition, but the flexibility cost is real and the performance advantage it used to hold over a swap file has largely evaporated on modern kernels and SSDs.
Swap file: the flexible modern default
A swap file is an ordinary file on your existing filesystem that you tell the kernel to use as swap. It does exactly the same job as a swap partition, but because it is just a file you can create it, resize it, or delete it at any time without touching your partition layout. On current Linux it performs essentially on par with a partition, so the old reasons to prefer a partition mostly no longer apply.
This is why the swap file is the sensible default for most installs today: you get all the benefits of swap without committing disk geometry up front. One caveat worth knowing if you went with btrfs as your filesystem (see the filesystems explainer): a swap file on btrfs needs a little extra setup, because copy-on-write and swap files do not mix by default. On ext4 there is nothing special to do. The Arch Wiki swap file section has the exact commands.
zram: compressed swap that lives in RAM
zram is the interesting one, and increasingly the modern default elsewhere. It is a kernel feature that creates a compressed block device inside RAM and uses it as swap. That sounds circular, but it works: when memory gets tight, the kernel "swaps out" the least-used pages by compressing them and keeping them in this in-RAM device, rather than writing them to slow disk. Because typical program memory compresses well, you effectively fit more into the same physical RAM, and because nothing touches the disk it is dramatically faster than disk-backed swap.
zram has become the standard approach on low-RAM machines and is now a default on several mainstream distributions. Fedora, for example, enables swap-on-zram by default, sized at 50% of physical RAM up to a cap. The trade-off is that zram is not real overflow capacity: it only buys you the compression ratio, so it does not save you when you genuinely run out of memory the way disk swap can. And it cannot help with hibernation: you cannot write RAM's contents into a device that itself lives in RAM and vanishes at power-off, so a system that hibernates still needs disk-backed swap as well.
How much swap do you need?
The old "twice your RAM" rule is obsolete; it dates from an era of tiny memory and is wrong for a modern machine with 16 or 32 GB of RAM, where it would waste a huge slice of disk for no benefit. Current guidance is more modest:
- For ordinary use without hibernation, a few gigabytes is plenty as a pressure valve. The Arch Wiki swap page declines to give a single number precisely because it depends on workload; a small fixed amount (say 2 to 8 GB) is a reasonable starting point on a typical desktop, and you can grow a swap file later if you find you need more.
- For hibernation, you need disk-backed swap at least as large as your RAM, since the whole memory image has to fit. The rule of thumb is 1:1 swap-to-RAM for reliable hibernation, as the Arch Wiki power management article notes, though in practice you can often get away with somewhat less because the image compresses. If you want hibernation to just work, size swap equal to RAM and do not overthink it.
ℹ️ Note Hibernation needs disk-backed swap at least as large as your RAM. zram cannot back it: you cannot write RAM's contents into a device that itself lives in RAM and vanishes at power-off, so a system that hibernates always needs a partition or file alongside.
If you run zram and want hibernation, you keep zram for everyday pressure and add a separate disk-backed swap (partition or file) sized for the memory image; the two coexist, with zram taking priority for normal swapping.
Which should I pick?
For a normal desktop or laptop that does not hibernate, the best default is a swap file, sized small (a few GB), or zram if you are on a machine with limited RAM where the speed and compression genuinely help. Both are easy to set up after install and easy to resize or remove. The swap file wins on simplicity and works everywhere; zram wins on speed and is the better pick when RAM is the constraint.
💡 Tip For a normal desktop, use a swapfile or zram. Both are easy to set up after install and easy to resize or remove: the swapfile wins on simplicity and works everywhere, while zram wins on speed when RAM is the tight constraint.
Choose a swap partition only if you have a specific reason to want one, such as a setup tool that expects it or a strong preference for keeping swap off your data filesystem. It is not wrong, just less flexible than a file for no real gain on modern hardware.
If you want hibernation, the form must be disk-backed (partition or file, not zram), and the size must be at least your RAM. Decide whether you care about hibernation before you size anything, because that single requirement is what drives the number.
The choice comes down to two questions answered in order: do you need hibernation, and is RAM your tight constraint. Everything else follows from those.
Pick by hibernation first, then by whether RAM is the constraint.
flowchart TD
A["Which swap form?"] --> B{"Need hibernation?"}
B -->|"yes"| C["Disk-backed swap<br/><i>size at least RAM, not zram</i>"]
B -->|"no"| D{"Is RAM tight<br/>on this machine?"}
D -->|"yes, low RAM"| E["zram<br/><i>fast, compressed</i>"]
D -->|"no, normal desktop"| F["Swapfile<br/><i>or zram, both fine</i>"]
A --> G{"Special setup<br/>that expects one?"}
G -->|"yes"| H["Swap partition"]
How this fits the Arch install
This is the decision behind the optional swap step in installing Arch part 1, where, after partitioning, you can create swap with mkswap and turn it on with swapon. If you took the default and skipped it, your system still boots and runs; you can add a swap file at any time later without repartitioning, which is exactly why the swap file is the recommendation here. The whole sequence is laid out in the Arch install companion hub.
A short close
Swap is overflow for when RAM fills up, plus the place hibernation stashes your memory image. A swap partition is the rigid classic, a swap file is the flexible modern default, and zram is fast compressed swap that lives in RAM and is becoming the default on distributions like Fedora. Ignore the old "twice your RAM" advice: a few gigabytes is fine for ordinary use, and the one case that forces a real number is hibernation, which needs disk-backed swap at least the size of your RAM. For most people a small swap file or zram is the right call, and you can always change it later.