How-to

Swap on Linux: Add It, Size It, Read It

On a machine with 64 GB or more, swap is not extra memory. It is a cushion that lets the kernel move idle pages aside and survive a spike without killing anything.

Do I need swap on a server with lots of RAM?

A small amount, yes, in most cases. Long-running servers accumulate memory pages that are touched once and never again: start-up code, rarely used libraries, idle sessions. With swap available, the kernel can move those out and use the RAM for cache. Swap also gives you time when memory spikes, so a slow, visible degradation replaces an abrupt OOM kill. What swap must never be is working memory: a box that reads and writes swap constantly is out of RAM, and the fix is a bigger machine, not a bigger swap file.

Does my machine have swap already?

swapon --show
free -h | grep -i swap

Empty output from swapon means no swap. Images differ, so check rather than assume.

How do I add a swap file?

A file is simpler than a partition and easy to resize. On current util-linux, mkswap can create the file itself, with the right size and permissions:

mkswap --file /swapfile --size 8G      # util-linux 2.39 and later (Ubuntu 24.04, Debian 13)
swapon /swapfile
printf '%s\n' '/swapfile  none  swap  defaults,pri=10  0 0' >> /etc/fstab

On older systems, such as Debian 12 or the Enterprise Linux 9 images, build it in three steps:

dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress
chmod 600 /swapfile
mkswap -L swap0 /swapfile && swapon --verbose /swapfile

dd works on every file system. fallocate is faster and works for swap on ext4 and XFS with current kernels, but dd never surprises you. On Btrfs, swap files need special handling; on ZFS, swap on a zvol is possible but often discouraged, so FreeBSD users typically use a swap partition.

How much swap should I add?

RAMSuggested swapReason
16 GB, as on E14 to 8 GBRoom for spikes on a small box
64 to 128 GB4 to 8 GBParking idle pages, early warning
256 GB and up4 to 16 GBSame purpose; more swap only delays the inevitable

Old rules like "twice the RAM" date from machines with a few hundred megabytes. On modern servers they waste disk and, worse, let a runaway process grind through gigabytes of swap for minutes before the OOM killer acts. Hibernation, the one case that needs swap at least the size of RAM, does not apply to servers.

How do I make the kernel prefer RAM?

Lower vm.swappiness. The default of 60 balances swapping program memory against dropping file cache; 10 keeps programs in RAM longer, a good fit for databases:

echo 'vm.swappiness = 10   # favor RAM for program memory' > /etc/sysctl.d/60-swappiness.conf
sysctl --system | grep swappiness

The broader picture of how the kernel balances memory is in Linux memory management.

What is zram and should I use it?

zram creates a compressed block device in RAM and uses it as swap. Pages that would go to disk are compressed instead, often at 2:1 or 3:1, and stay in memory. It is excellent on small machines and desktops; Fedora and some others enable it by default. On a large server it is less useful, since the problem it solves, too little RAM, is rare there, but it costs nothing to try. Install systemd-zram-generator or zram-tools and set a size such as a quarter of RAM.

How do I tell healthy swap use from thrashing?

vmstat 5 6

Look at the si (swap in) and so (swap out) columns. A few gigabytes sitting in swap with both columns near zero is healthy: idle pages were parked and stay parked. Continuous non-zero values in both columns mean active memory is being shuffled to disk and back, which is thrashing. Confirm with cat /proc/pressure/memory, and see Check memory usage for more checks.

How do I remove or resize swap?

swapoff /swapfile
rm /swapfile
sed -i '\#^/swapfile #d' /etc/fstab

swapoff moves swapped pages back into RAM first, so it needs enough free memory and can take a while on a busy box. To resize, turn it off, recreate the file with the new size and turn it back on.

Keep Reading

Check memory usage

Read free, /proc/meminfo, vmstat and per-process numbers correctly, and tell real memory pressure apart from a healthy page cache.

Open

Linux memory management

Virtual memory, pages, overcommit, huge pages, cgroup limits and the OOM killer: how Linux hands out RAM and the knobs worth turning.

Open

Format a disk on Linux

Turn the blank second NVMe drive into usable space: identify it, partition with GPT, choose ext4 or XFS, mount by UUID and keep TRIM on.

Open

Entry Xeon servers

The cheapest way onto real hardware, from $30.52 a month.

Open

FAQ

Quick Answers

A small swap file still helps: it parks idle pages and softens memory spikes. It should never be used as working memory.

Use mkswap --file /swapfile --size 8G on newer util-linux, or dd, chmod 600, mkswap and swapon on older systems.

4 to 8 GB suits most servers from 16 GB to 128 GB of RAM; very large machines rarely need more than 16 GB.

The default 60 is fine for general use; around 10 suits database servers.

vmstat shows steady non-zero values in both si and so columns, and memory pressure in /proc/pressure/memory rises.