Swap Space Explained

Swap Space Explained

Swap is disk space the Linux kernel uses as an extension of RAM, a place to temporarily hold memory pages that are not being actively used so physical RAM stays available for what actually needs it right now. It is not a substitute for RAM (disk is orders of magnitude slower than memory), but a pressure valve that lets a system degrade gracefully under memory pressure instead of killing processes outright.

What swap actually does

When available RAM gets tight, the kernel identifies memory pages that have not been accessed recently and writes them out to swap, freeing that RAM for more immediately active use. If a swapped-out page is needed again later, the kernel reads it back in from swap, a much slower operation than accessing RAM directly, which is why heavy sustained swapping (often called “thrashing”) causes a system to feel sluggish or unresponsive.

Swap also underpins hibernation. When a system hibernates, it writes the entire contents of RAM to disk before powering off, and on most Linux systems that data goes into swap space, which is why swap generally needs to be at least as large as installed RAM if hibernation support matters to you.

Swap partition vs swapfile

Linux supports two forms of swap: a dedicated disk partition formatted specifically as swap, or a regular file on an existing filesystem used as swap space.

On modern systems with SSDs and current kernels, the performance difference between the two is minimal for typical use. Swapfiles are generally more convenient, since resizing a swapfile does not require repartitioning the disk, which is why most current distribution installers default to a swapfile rather than a dedicated partition. A dedicated partition still has a place for certain hibernation configurations and older setups, but for most users a swapfile is the simpler, equally effective choice.

Checking current swap status

free -h
#                total        used        free      shared  buff/cache   available
# Mem:            15Gi       6.2Gi       3.1Gi       412Mi       6.1Gi       8.5Gi
# Swap:          4.0Gi          0B       4.0Gi

swapon --show
# NAME      TYPE      SIZE  USED PRIO
# /swapfile file        4G    0B   -2

swapon --show lists each active swap device or file, its size, how much is currently used, and its priority (higher priority swap is used before lower priority swap, if multiple swap areas exist).

Creating a swapfile

sudo fallocate -l 4G /swapfile      # allocate a 4GB file
sudo chmod 600 /swapfile             # restrict permissions, swap should not be world-readable
sudo mkswap /swapfile                # format it as swap
sudo swapon /swapfile                # activate it immediately

If fallocate is not available or produces errors on your filesystem, dd works as a slower fallback:

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

To make the swapfile persist across reboots, add it to /etc/fstab:

/swapfile  none  swap  sw  0  0

Without this fstab entry, the swapfile you just created and activated will not be re-enabled automatically the next time the system boots.

Creating a dedicated swap partition

If you have a spare partition (created during initial partitioning or with a tool like fdisk or parted):

sudo mkswap /dev/sdX1        # format the partition as swap
sudo swapon /dev/sdX1         # activate it

Add it to /etc/fstab for persistence, ideally by UUID rather than device name:

sudo blkid /dev/sdX1
UUID=your-uuid-here  none  swap  sw  0  0

Adjusting swappiness

Swappiness is a kernel tunable from 0 to 100 controlling how aggressively the kernel favors swapping versus reclaiming page cache. Higher values swap more readily; lower values delay swapping in favor of other memory reclaim strategies.

cat /proc/sys/vm/swappiness    # check current value (default is usually 60)

sudo sysctl vm.swappiness=10   # change immediately (does not survive reboot)

To make a swappiness change permanent, add it to /etc/sysctl.conf or a file in /etc/sysctl.d/:

echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf

Desktop and laptop users on systems with ample RAM often lower swappiness to reduce noticeable stalls from swap activity. Server workloads vary more, and tuning should generally be based on observed behavior under real load rather than a one-size-fits-all number.

How much swap do you actually need

The traditional advice of “swap equal to twice your RAM” dates from an era of far smaller RAM sizes and is largely outdated. For a modern desktop or laptop with 8GB or more of RAM, 2 to 4GB of swap is usually enough to absorb memory spikes. If hibernation matters, size swap to be at least as large as installed RAM. For servers, sizing depends heavily on the specific workload, and many production systems run modest swap paired with active memory monitoring rather than relying on a large swap area as the primary defense against memory exhaustion.

Frequently Asked Questions

What is swap used for on Linux?

Swap is disk space the kernel uses as an overflow area for memory, holding pages that are not being actively used so that physical RAM can be freed up for things that need it right now. When the system runs low on free RAM, the kernel moves the least recently used memory pages out to swap, making room without killing processes outright. Swap is also required for hibernation on most systems, since hibernating saves the entire contents of RAM to disk, and swap is typically where that gets written.

Is a swap partition better than a swapfile?

On modern Linux systems with SSDs and reasonably current kernels, the performance difference between a dedicated swap partition and a swapfile is negligible for most use cases. A swapfile is more flexible, since it can be resized without repartitioning the disk, which is why most current distribution installers default to swapfiles rather than dedicated partitions. A dedicated swap partition is still preferred in a few specific cases, such as systems that need to hibernate reliably, where some configurations require a partition rather than a file, or setups predating widespread swapfile support.

How much swap space do I actually need?

There is no single correct answer, and the old rule of thumb of “swap equal to twice your RAM” is largely outdated advice from an era of much smaller RAM sizes. For a desktop or laptop with 8GB or more of RAM, 2 to 4GB of swap is often enough to smooth over memory spikes without expecting heavy sustained use. For hibernation support, swap generally needs to be at least as large as your RAM, since the entire contents of memory get written there. For servers, sizing depends heavily on the workload; many production servers run with modest swap (or none at all) paired with monitoring to catch memory pressure before it becomes a problem, rather than relying on swap as the primary buffer.

What does swappiness control and what should I set it to?

Swappiness is a kernel tunable from 0 to 100 that controls how aggressively the kernel moves memory pages to swap versus reclaiming them from the page cache instead, both of which free up RAM but affect performance differently. A higher value makes the kernel swap more readily; a lower value makes it prefer reclaiming cache and delaying swap use as long as possible. The Linux default is usually 60. Desktop users on systems with plenty of RAM often lower it to 10 to 20 to reduce perceptible swapping (which can cause momentary stalls when swapped-out data is needed again), while some server workloads leave it at the default or adjust it based on measured behavior rather than a blanket rule.

Why is my system swapping even though I have free RAM?

Some swap usage even with apparently free RAM is normal and not necessarily a problem: the kernel proactively swaps out memory pages that have been idle for a long time, freeing that RAM for disk cache and other more immediately useful purposes, on the theory that idle pages are unlikely to be needed again soon. This is different from thrashing, where the system is actively swapping pages in and out repeatedly because it does not have enough RAM for its current workload, which shows up as sustained high swap I/O and sluggish performance. Check free -h and watch the swap row over time, along with vmstat 1, to distinguish light background swapping from genuine memory pressure.

How do I check how much swap is being used?

free -h shows total, used, and free swap alongside RAM in a human-readable format. swapon —show lists each active swap device or file along with its size, used amount, and priority. For a live, continuously updating view, vmstat 1 shows swap activity (the si and so columns, for swap-in and swap-out) updated every second, which is useful for distinguishing a one-time swap event from ongoing swap pressure.