Stop making swap partitions—use swap files instead
🛑 Stop making swap partitions—use swap files instead!
By joshenders
For over two decades, the performance gap between swap files and swap partitions has effectively vanished. Despite this, many Linux distributions still push users toward creating dedicated swap partitions during the initial installation process.
The reality is that swap files are far more versatile. They are significantly easier to implement, expand, shrink, or delete entirely after the OS is already running.
🛠️ Implementation Guide
If your filesystem supports it, fallocate is the preferred method because it is faster than dd.
Standard Setup:
# Create a 4GB swap file
fallocate -l 4G /swapfile
# Ensure only root has write access for security
chmod 600 /swapfile
# Make it permanent via fstab
echo " /swapfile none swap defaults 0 0 " >> /etc/fstab
For Btrfs Users: Since Btrfs handles files differently, use the dedicated tool:
btrfs filesystem mkswapfile --size 2G swapfile
swapon swapfile
Reference: Btrfs Swapfile Documentation
⚖️ The Great Swap Debate
The community is divided on this approach. Below is a conceptual map of the arguments:
🚩 The Skeptic's View
sgbeal raises several critical points:
- Performance: He questions the "20-year" claim and argues that because any filesystem introduces an abstraction layer (and thus a penalty).
- Necessity: In 30 years of Linux/Solaris admin, he claims he has
neverrarely needed to resize swap post-install. - Hygiene: Partitions are "cleaner" because they don't accidentally get sucked into
rsyncjobs or system backups.
🚩 The System Architect's View
DominoTree (FreeBSD committer/ZFS contributor) adds technical nuance:
"As long as you're okay with swap becoming available after your filesystems do... there are cases where you might not want that."
Critical Risks:
- Boot-time OOM: On memory-constrained systems using Copy-on-Write (CoW) filesystems (which consume RAM), you might hit an Out-Of-Memory (OOM) state before the filesystem is mounted and the swap file becomes available.
- Kernel Dumps: In FreeBSD, swap partitions double as kernel dump devices. If a panic occurs before ZFS is online, a swap file is useless for storing the crash dump.
⚠️ The ZFS Warning
Harvie and DominoTree highlight a dangerous interaction with ZFS.
| Feature | Issue | Result |
|---|---|---|
| ZFS ARC | Consumes massive amounts of RAM | High memory pressure |
| ZFS Swap | Swapping ZFS pages back into a ZFS-backed file | |
| Outcome | System instability | Freeze or Crash |
Note: This can be mitigated with aggressive sysctl tuning, but is dangerous on default settings.
📝 Final Considerations
- Check your FS: Does it support
fallocate? - Check your Boot Order: Do you need swap before the root FS mounts?
- Check your Backups: If using a file, is it excluded from your backup scripts?
- Check your OS: Are you on FreeBSD or using ZFS?
Other perspectives:
Ludwik900 suggests ensuring
/etcconfigurations are properly set for system startup.zenoprax notes that you can simply keep an existing swap partition intact even when reinstalling the OS, especially if it's on a separate "DMZ" SSD.
hecvd agrees with the "sysadmin hygiene" argument regarding backups.