Skip to content
Go back

Adding Extra Swap to Linux

Updated:
By SumGuy 5 min read
Adding Extra Swap to Linux

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)

Terminal window
$ sudo fallocate -l 8G /swapfile
$ ls -lh /swapfile
-rw-r--r-- 1 root root 8.0G Apr 5 14:23 /swapfile

fallocate 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)

Terminal window
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
8192+0 records in
8192+0 records out
8589934592 bytes (8.6 GB, 8.0 GiB) copied in 45.2234 s, 190 MB/s

This 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):

Terminal window
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=a1b2c3d4-e5f6-7g8h-9i0j-k1l2m3n4o5p6

Now enable it:

Terminal window
$ sudo swapon /swapfile
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 8.0G 0B -2

Done. Your swap is live.

Making It Permanent (fstab)

Everything above lasts until reboot. To make it permanent, add an entry to /etc/fstab:

Terminal window
$ sudo nano /etc/fstab

Add this line at the bottom:

/swapfile none swap sw 0 0

Save and exit. On next boot, the kernel will activate /swapfile automatically.

To verify it’ll work without rebooting:

Terminal window
$ sudo swapon -a # activate all swap from fstab
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 8.0G 0B -2

Tuning Swappiness

By default, Linux tries to avoid swap until pressure gets bad. This is controlled by swappiness, a value from 0–100.

For servers and LLM boxes, I usually drop it to 10:

Terminal window
$ cat /proc/sys/vm/swappiness
60
$ sudo sysctl vm.swappiness=10
vm.swappiness = 10
$ cat /proc/sys/vm/swappiness
10

To make it permanent, add to /etc/sysctl.conf:

vm.swappiness=10

Then reload:

Terminal window
$ sudo sysctl -p
vm.swappiness = 10

Modern 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:

Watch your usage:

Terminal window
$ free -h
total used free shared buffers cached
Mem: 15Gi 12Gi 2.5Gi 256Mi 100Mi 4Gi
Swap: 8.0Gi 512Mi 7.5Gi

If 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:

Terminal window
$ watch -n 1 'free -h; echo; swapon --show'

Or get detailed breakdown:

Terminal window
$ vmstat 1 5
procs -----------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 0

The 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.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Linux Privilege Escalation: The Defensive Playbook
Next Post
Bash for loops sequential counting

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts