FineShot.AI
•6 min read

Linux Freezing Under Load? Fix Your Swap (4 GB to 16 GB, the Right Way)

Is your Linux desktop freezing up when things get busy? Before you blame the RAM, check your swap. Nine times out of ten, that’s the culprit.

Here’s what swap actually is, how Linux uses it, why it was painful on old hard drives but is a real safety net on NVMe, and how we resized it on a real machine: 4 GB to 16 GB, including a fallocate gotcha that trips a lot of people up.

Watch the video

The explanation with animations, then the exact commands live, ending with swap-in and swap-out happening in real time:

What is swap?

Your apps live in RAM. It’s fast, but there’s only so much of it. Swap is spare memory that lives on your disk.

When RAM fills up, Linux quietly moves the coldest data, the stuff you’re not using right now, out to swap. Instead of slamming into a wall and crashing, your system keeps running.

Diagram: RAM overflows into swap on disk
When RAM is full, cold data overflows into swap on the disk.

How Linux uses it

  • Swap-out: when RAM gets tight, the kernel writes cold pages out to disk.
  • Swap-in: when that data is needed again, it’s read back into RAM.
  • swappiness: one number, from 0 to 100, that controls how eagerly Linux reaches for swap. The default is 60. Lower it, and Linux holds on to RAM longer and only swaps when it really has to.

The catch: disk is slow

Swap sits on disk, and disk is far slower than RAM:

Memory / storageRough speed
RAM~40 GB/s
NVMe SSD~5 GB/s
SATA SSD~0.5 GB/s
Hard drive~0.15 GB/s
Speed bars: RAM 40 GB/s, NVMe 5 GB/s, SATA SSD 0.5 GB/s, hard drive 0.15 GB/s
Swap sits on disk, which is far slower than RAM.

A little swapping, and you’ll never notice. But constant swapping is called thrashing: the disk can’t keep up, the CPU sits waiting, and everything crawls or freezes.

Why NVMe changes the game

On an old spinning hard drive, every swap was a stall and the whole machine felt frozen. NVMe is a different world: gigabytes per second, so swapping stays smooth. On NVMe, a healthy swap isn’t a crutch. It’s a real safety net that buys you time instead of a crash.

Old hard-disk swap at 150 MB/s versus NVMe swap at gigabytes per second
Spinning disks made swapping painful. NVMe keeps it smooth.

How much swap do you need?

You’ve probably heard the old rule: swap should be twice your RAM. With 24 GB of RAM, that would be 48 GB of swap. That’s overkill today. With this much memory you barely swap at all, so a flat 16 GB is plenty as a safety net.

Our machine had 24 GB of RAM but only 4 GB of swap, left over from its 8 GB days, so the swap kept filling up and stalling. 16 GB is the sweet spot here.

Pick the size for your own workload, not a fixed number. Use more if you run memory-heavy apps, VMs or lots of browser tabs. If you want hibernation, swap needs to be at least the size of your RAM. Use less if you have huge RAM and just want a small cushion.

The fix, step by step

These are the exact commands we ran. Our swap file is /swap.img (the Ubuntu default). Check your own path with swapon --show first and change it in every command if yours is different.

1. See what you have. Four gigs of swap, and it’s almost full.

free -h
swapon --show
free -h shows 4.0Gi swap with 1.9Gi used; swapon --show lists /swap.img at 4G
4 GB of swap, almost full: way too small for this machine.

2. Turn the current swap file off so it can be resized safely. This moves everything in swap back into RAM, so it can take a moment. Make sure you have enough free RAM first.

sudo swapoff /swap.img

3. The fallocate gotcha. The obvious move is fallocate, because it’s instant. On our machine we formatted the file, tried to switch it on, and got: swapon failed: Device or resource busy. fallocate only reserves space and can leave gaps in the file, and the kernel refuses a swap file with holes. Some filesystems handle it fine, but dd works everywhere.

4. Rebuild the file for real with dd, which writes 16 GiB of actual zeros, so there are no holes:

sudo dd if=/dev/zero of=/swap.img bs=1M count=16384 status=progress

count is the size in MiB: size in GiB × 1024. Double-check the of= path, because dd overwrites whatever file you point it at.

swapon failed: Device or resource busy after fallocate; dd rebuild writes 16 GiB
fallocate’s file was refused. dd writes real zeros and works.

5. Lock it down, format it and switch it on. Swap should only be readable by root:

sudo chmod 600 /swap.img
sudo mkswap /swap.img
sudo swapon /swap.img

6. Confirm with free -h: swap now shows 16 GiB.

7. Make it survive a reboot. Check /etc/fstab for a line pointing at the swap file. On our machine it was already there. If yours isn’t, this adds it once:

grep -q '/swap.img' /etc/fstab || echo '/swap.img none swap sw 0 0' | sudo tee -a /etc/fstab

8. Swap less eagerly. Set swappiness to 10 instead of the default 60, so Linux holds on to RAM longer:

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system | grep swappiness

Don’t set it to 0 on a desktop, because that makes Linux avoid swap until it’s almost too late.

free -h shows 16Gi swap; /etc/fstab already has the swap line; setting swappiness to 10
16 GiB of swap, already in fstab, and swappiness going to 10.

Watch swap work, live

Now the fun part. vmstat prints memory activity every second. Watch two columns: si is swap-in and so is swap-out, both in KB/s.

vmstat 1 5
iostat -x 1 2 nvme0n1

As memory gets tight, so climbs as pages head out to disk. Then it spikes, swapping hard, and si jumps as pages are read back in. Then it calms down to zero. The spike got absorbed instead of freezing the machine.

And on the NVMe itself, iostat‘s last column, %util, showed about 4% busy while swapping. An old spinning drive would be pinned at 100% and the whole system would hang. The NVMe just shrugs it off.

vmstat showing si/so spikes then calm, and iostat showing nvme0n1 at 4.2% util
Swap-out, swap-in, then calm, with the NVMe only about 4% busy.

iostat comes from the sysstat package. If it’s missing: sudo apt install sysstat.

Summary

  • Swap is spare memory on disk. It keeps Linux running when RAM fills up.
  • On NVMe it’s a real safety net, not the performance killer it was on hard drives.
  • The old “2× RAM” rule is overkill with lots of RAM. Size swap for your workload; we went from 4 GB to 16 GB.
  • Resize with swapoff → dd → chmod 600 → mkswap → swapon, keep it in /etc/fstab, and set swappiness to 10.

Everything here was done on our own machine. Commands run as root can do real damage, so double-check paths and sizes for your system before running them, and make sure you have a backup.


More on YouTube

Prefer watching? The full swap walkthrough is on our YouTube channel: watch the video. For more hands-on breakdowns of security bugs, Linux servers and dev tools, subscribe at youtube.com/@fineshotai.