How to Increase Linux Swap Space

How to Increase Linux Swap Space

Running out of swap space on Linux can cause sluggish performance, out-of-memory kills, and failed suspend-to-disk. Whether you are running a memory-hungry database or just want a safety buffer, this guide shows you how to add or grow swap using both swap files and partitions.

Check Your Current Swap

Before changing anything, confirm how much swap you already have and where it lives. Two commands tell you almost everything:

  • swapon –show lists active swap devices, their type (partition or file), size, and usage.
  • free -h shows total RAM and swap in human-readable units.

You can also inspect /proc/swaps directly for the same device list. If swapon --show returns nothing, you currently have no swap active at all.

Add Swap Using a Swap File

A swap file is the most flexible option because it does not require repartitioning. The modern, reliable way to create one on most distributions:

  1. Allocate the file: sudo fallocate -l 4G /swapfile. If fallocate fails on your filesystem (notably some XFS or ZFS setups), use sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 instead.
  2. Lock down permissions so only root can read it: sudo chmod 600 /swapfile.
  3. Format it as swap: sudo mkswap /swapfile.
  4. Enable it immediately: sudo swapon /swapfile.

To make the swap file survive reboots, add a line to /etc/fstab:

/swapfile none swap sw 0 0

Note that swap files do not work on Btrfs the same way. On Btrfs you must create the file with btrfs filesystem mkswapfile (kernel 5.x+) or set the file to no-copy-on-write before formatting.

Increase the Size of Existing Swap

You cannot simply append to an active swap file. The correct sequence is to turn it off, recreate it at the larger size, and turn it back on:

  1. sudo swapoff /swapfile (this can take time if swap is heavily used, since the kernel must move pages back into RAM).
  2. sudo rm /swapfile then recreate it larger with the steps above.
  3. sudo swapon /swapfile to re-enable.

For a swap partition, you would resize the partition with a tool like parted or gparted, then run mkswap and swapon again. Resizing a partition is riskier, so swap files are usually easier for incremental growth.

Tune Swappiness and Verify

How aggressively Linux uses swap is controlled by vm.swappiness, a value from 0 to 100. The default is often 60. Lower values (10 to 20) keep more data in RAM and only swap under pressure, which suits desktops and latency-sensitive servers. Set it live with:

sudo sysctl vm.swappiness=10

Make it permanent by adding vm.swappiness=10 to /etc/sysctl.conf or a file in /etc/sysctl.d/. After everything is in place, run swapon --show and free -h again to confirm the new total. For systems using zram or zswap, remember those are compressed RAM-based swap layers and complement, rather than replace, disk swap.

Frequently Asked Questions

How much swap space should I allocate?

A common guideline is equal to RAM for systems up to 8 GB, then half of RAM beyond that. If you need hibernation (suspend-to-disk), swap must be at least as large as your RAM. For pure performance buffering, 2 to 4 GB is often enough.

Is a swap file slower than a swap partition?

On modern kernels and SSDs the performance difference is negligible. Swap files were historically slower due to filesystem overhead, but that gap has largely disappeared. Swap files are far easier to resize, which usually makes them the better choice.

Can I add swap without rebooting?

Yes. After running mkswap and swapon, the new swap is active immediately with no reboot required. The reboot only matters for persistence, which is handled by your /etc/fstab entry.

Why is my swapoff command hanging?

swapoff must move every swapped-out page back into RAM before it can disable the device. If swap is full and RAM is tight, this can take a long time or fail with an out-of-memory error. Free up memory first or close large applications before disabling swap.

Similar Posts