When Swap Actually Matters
Here’s the thing about swap: most people either ignore it or obsess over it. The reality is somewhere in the middle. Swap won’t save you from terrible code or memory leaks, but it will save you when you’re running LLM inference on a 8GB home lab box, or when your memory-constrained VM suddenly gets a traffic spike.
The classic advice of “swap = 2x RAM” is dead. That was written when you had 512MB of RAM. Today? You’re more likely to run out of disk space before you need swap that large. But you should have some swap — it’s cheap insurance and keeps your system from hard-locking when memory pressure spikes.
Swapfile vs. Swap Partition
A swap partition is formatted disk space dedicated entirely to swap, allocated at install time. It’s slightly faster (no filesystem overhead) but inflexible — you can’t grow it without repartitioning.
A swapfile is just a regular file on your filesystem (living on / or any partition). It’s easier to resize, easier to delete, and easier to add after the fact. The performance difference is negligible on modern SSDs. For almost every case, use a swapfile.
Creating a Swapfile — Step by Step
Let’s create an 8GB swapfile. You have two tools: dd and fallocate. I’ll show you both.
Using fallocate (faster, preferred)
$ sudo fallocate -l 8G /swapfile$ ls -lh /swapfile-rw-r--r-- 1 root root 8.0G Apr 5 14:23 /swapfilefallocate is instant because it doesn’t actually write zeros — it just reserves space. Much faster than dd on large files.
Using dd (if fallocate isn’t available)
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=81928192+0 records in8192+0 records out8589934592 bytes (8.6 GB, 8.0 GiB) copied in 45.2234 s, 190 MB/sThis writes actual zeros, so it takes longer. But it works everywhere.
Set permissions and initialize
Swap files should be owned by root with 600 permissions (no one else can read them):
$ sudo chmod 600 /swapfile$ sudo mkswap /swapfileSetting up swapspace version 1, size = 8 GiB (8589930496 bytes)no label, UUID=a1b2c3d4-e5f6-7g8h-9i0j-k1l2m3n4o5p6Now enable it:
$ sudo swapon /swapfile$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 8.0G 0B -2Done. Your swap is live.
Making It Permanent (fstab)
Everything above lasts until reboot. To make it permanent, add an entry to /etc/fstab:
$ sudo nano /etc/fstabAdd this line at the bottom:
/swapfile none swap sw 0 0Save and exit. On next boot, the kernel will activate /swapfile automatically.
To verify it’ll work without rebooting:
$ sudo swapon -a # activate all swap from fstab$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 8.0G 0B -2Tuning Swappiness
By default, Linux tries to avoid swap until pressure gets bad. This is controlled by swappiness, a value from 0–100.
- 0 = Use swap only when absolutely necessary (memory pressure > 99%)
- 60 = Default; balanced between RAM and swap
- 100 = Aggressively use swap
For servers and LLM boxes, I usually drop it to 10:
$ cat /proc/sys/vm/swappiness60
$ sudo sysctl vm.swappiness=10vm.swappiness = 10
$ cat /proc/sys/vm/swappiness10To make it permanent, add to /etc/sysctl.conf:
vm.swappiness=10Then reload:
$ sudo sysctl -pvm.swappiness = 10Modern Alternatives: zswap and zram
On newer kernels (5.0+), you have zswap and zram — compressed in-memory swap that sits between RAM and disk swap.
zswap compresses data in RAM using zstd before sending it to disk. It reduces actual disk I/O but costs CPU.
zram creates a compressed RAM disk. You can allocate 2GB of fake swap from half the RAM. Useful for squeeze scenarios.
Both reduce disk thrashing but add CPU overhead. On a loaded system, that tradeoff is worth it. For simple setups, traditional swap is fine.
How Much Swap Is Enough?
The old rules are gone. Here’s what actually matters:
- Servers and VMs: 1–2GB minimum. More if you’re running memory-heavy workloads (Elasticsearch, LLMs, databases). Rule of thumb: 25% of RAM.
- Workstations: 2–4GB. Gives you breathing room for browser tabs and Electron apps eating everything.
- Embedded/home lab: Start with 2GB. Add more only if you see swap getting used regularly.
Watch your usage:
$ free -h total used free shared buffers cachedMem: 15Gi 12Gi 2.5Gi 256Mi 100Mi 4GiSwap: 8.0Gi 512Mi 7.5GiIf you’re consistently hitting swap, you either need more RAM or a code audit. If swap is sitting unused, that’s fine — it’s just insurance.
Monitoring Swap
Check real-time usage:
$ watch -n 1 'free -h; echo; swapon --show'Or get detailed breakdown:
$ vmstat 1 5procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 524288 2.5G 100M 4.0G 10 20 50 150 500 800 45 10 40 5 0The si and so columns (swap in/out) tell you if the kernel is actually using swap. If si and so are both 0, swap isn’t being touched.
One More Thing
If you’re on a VM and the hypervisor is already ballooning or overcommitting memory, swap won’t help — it’ll just be slow disk I/O. But on bare metal or properly-sized VMs? Swap is your friend. Set it up, forget it, and sleep better knowing you won’t get OOM-killed at 2 AM.