What happens when a server's physical memory (RAM) fills up? This is exactly where swap space comes in. Swap provides a temporary expansion area on disk when RAM runs short. This guide explains what swap does, how it is configured and its effect on performance.

Related reading: Essential Linux server commands · Linux package management

What Is Swap Space?

Swap is the area where the operating system temporarily moves rarely-used memory pages to disk when physical RAM fills up. This way, even if RAM is completely full, processes can keep running instead of crashing. Swap can be defined as a separate disk partition or as a swap file.

The key point is this: disk is far slower than RAM. Swap is a safety net, not a RAM replacement. A server that constantly uses swap actually needs more RAM.

What Does Swap Do?

  • Memory overflow protection: Prevents processes from crashing with OOM (Out of Memory) when RAM fills up.
  • Freeing rarely-used data: Moves long-untouched memory pages to disk, freeing RAM for active work.
  • Burst load buffer: Lets the system breathe during short-lived memory spikes.
  • Hibernate: On desktop systems, used by writing memory content to swap — rare on servers.

Creating a Swap File

On modern systems, using a swap file instead of a separate partition is both flexible and practical. A 2 GB swap file is created like this:

# Create a 2 GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Add to /etc/fstab to make it persistent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

You can verify that swap is active with free -h or swapon --show.

The swappiness Setting

swappiness is a value between 0 and 100 that determines how eagerly the kernel uses swap. The default is usually 60. On servers a lower value (for example 10) makes the system resort to swap only when truly necessary:

# Immediate change
sudo sysctl vm.swappiness=10

# Add to /etc/sysctl.conf to make it persistent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

How Much Swap Do You Need?

There is no strict rule, but a common approach is: for RAM up to 2 GB, twice the RAM; for 2-8 GB, equal to RAM; on systems above 8 GB, a fixed 2-4 GB is enough for most servers. The real solution, if you see constant swap usage, is to add RAM.

Warning
If a server constantly and heavily uses swap, its performance drops significantly — this is called "swap thrashing". It is not a configuration problem but a sign of insufficient resources; consider upgrading RAM.

Frequently Asked Questions

Does using swap on an SSD damage the disk?

Modern SSDs withstand a large number of write cycles; normal swap usage causes no practical problem. A low swappiness value already reduces unnecessary writes.

Should I disable swap entirely?

Even with plenty of RAM, a small swap acts as a safety net during sudden load spikes. It is generally recommended to keep it with a low swappiness rather than disabling it.

Is a swap file or a partition better?

The performance difference is negligible on modern systems. Because a swap file's size is easy to change, it is more practical in most scenarios.

The Right RAM for Your Needs

With KEYDAL VPS plans, your server stays stable under load with RAM and NVMe SSD suited to your project. Explore KEYDAL hosting

WhatsApp