3.3 Swap: Partition vs Swap File, Sizing Guidelines
Right, swap. The great debate. Let’s get one thing straight: swap isn’t “extra RAM.” That’s like calling a lifeboat an “extra deck.” It’s an emergency measure, a place to shove idle data from your precious, blazing-fast RAM onto the comparatively glacial pace of your disk drive. The goal isn’t to make things faster; it’s to keep your system from face-planting when memory gets tight. The question is, how do you provision this lifeboat? A dedicated partition, or a humble file?
The Partition vs. File Holy War
For decades, a dedicated swap partition was the only way. It was carved out of the raw disk, and the kernel could talk to it directly. This had a certain elegance and performance benefit, especially on spinning rust (HDDs). Then, around 2012-2013, the kernel maintainers got their act together and made swap files not just possible, but actually robust. The performance difference today, especially on SSDs, is negligible for most use cases.
So why would you ever use a partition now? Two main reasons:
- Hibernation (suspend-to-disk): Some bootloaders and initramfs tools can still be fussy about reading the hibernation image from a swap file. A partition is universally supported. If you want to hibernate your laptop, just make a partition. It’s one less thing to debug at 2 AM.
- Filesystem Frailty: A swap file is, well, a file. It depends on the integrity of the filesystem it’s on. If your ext4 or XFS partition gets corrupted, your swap file is toast too. A swap partition is a separate entity; one less thing to go wrong in a catastrophe. (Though if your root FS is that corrupted, you’ve probably got bigger problems).
For 95% of you, especially on servers and desktops where you don’t need hibernation, a swap file is the way to go. It’s flexible. You can resize it, add more, or remove it without repartitioning your entire damn disk. It’s a modern solution.
Sizing Guidelines: The “It Depends” Special
Anyone who gives you a single formula like “double your RAM!” is selling something, probably a time machine back to 1998. The right amount of swap is a policy decision, not a calculation.
- The Traditionalist (and Hibernation) Rule: If you plan to hibernate, you must have swap space equal to or greater than your total physical RAM. The kernel needs a place to dump the entire contents of memory. So, if you have 16GB of RAM, you need at least a 16GB swap partition.
- The Minimalist / “I Have a Ton of RAM” Rule: On a server with 64GB of RAM that just runs a few well-behaved services, you might get away with a tiny 1GB swap—or even none at all. Its purpose shifts from a safety net to a trigger for the Out-Of-Memory (OOM) killer. Having a small swap allows the kernel to start moving truly idle junk to disk before it has to start shooting processes in the face. It’s a warning bell.
- The Realist Rule (My Preference): For a general-use system (desktop or server), start with a base amount for the OOM-killer warning bell, and then add a percentage of your RAM to account for real memory pressure.
sqrt(RAM)is a strangely sane starting point. For 16GB RAM, that’s 4GB. For 64GB, that’s 8GB. It’s not too much, not too little.
The best way to check your current usage is swapon --show to see what’s active and then free -h to see how much you’re actually using.
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 64G 0B -2
$ free -h
total used free shared buff/cache available
Mem: 62Gi 5.1Gi 54Gi 1.1Gi 3.5Gi 56Gi
Swap: 64Gi 0B 64Gi
See? This system has a comically large 64GB swap file and isn’t using a byte of it. It’s overkill, but it’s harmless.
Creating and Managing a Swap File
This is where the swap file wins on simplicity. No fdisk, no partition tables. Let’s create a 4GB file.
# Create the file itself. 'bs=1M' and 'count=4096' means 1MB * 4096 = 4GB.
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
# Set restrictive permissions. This is a security must.
sudo chmod 600 /swapfile
# Format the file as swap. Yes, it has its own "filesystem."
sudo mkswap /swapfile
# Turn it on immediately.
sudo swapon /swapfile
To make it permanent, add a line to your /etc/fstab:
# /etc/fstab entry for the swapfile
/swapfile none swap defaults 0 0
Tuning Swappiness: Calming the Kernel Down
The kernel has a setting called vm.swappiness (0 to 100) that controls its trigger-happiness for using swap. A higher value means it will more aggressively move data to swap even if there’s still free RAM. The default is usually 60, which is often too high for systems with ample RAM.
If your free command constantly shows a few megabytes of swap used, even with plenty of free memory, your kernel is just being a bit neurotic. You can check your current value with:
cat /proc/sys/vm/swappiness
To change it temporarily (until reboot):
sudo sysctl vm.swappiness=20
To make it permanent, add this line to /etc/sysctl.conf or a file in /etc/sysctl.d/:
vm.swappiness=20
A value of 10-30 is sensible for most modern systems with more than 8GB of RAM. It tells the kernel: “Hey, only use swap if we’re really in a pinch.” On databases or high-performance servers, you might even set it to 1. Experiment. It’s not voodoo, just a preference knob.