Linux SWAP Configuration Guide: A “Spare Tire” for Low-RAM Servers

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

The Nightmare Called Out of Memory (OOM)

It’s a common sight: a VPS with 1GB of RAM running 3-4 Docker containers suddenly freezes, the website returns a 502 error, and system logs are filled with notifications that the OOM killer has “slain” MySQL. This situation is extremely common when running heavy applications on low-spec servers.

After 5 years of system administration, I always set up SWAP as soon as I initialize a new server. While the speed can’t compare to physical RAM (it’s about 10-100 times slower depending on the drive type), SWAP acts as a safety buffer. It prevents the system from crashing unexpectedly during traffic spikes or memory leaks.

Swap Partition or Swap File?

Before typing any commands, you need to determine the deployment method that fits your existing infrastructure.

1. Swap Partition

This method involves setting aside an entire disk partition to act as a memory buffer.

  • Pros: Optimal performance because data is written sequentially to physical disk blocks.
  • Cons: Lack of flexibility. Expanding SWAP capacity requires modifying the partition table, which is risky for live servers.

2. Swap File

This method creates a large file directly on the existing partition to serve as virtual memory.

  • Pros: Absolute flexibility. You can increase SWAP from 2GB to 4GB in just 30 seconds without rebooting the server.
  • Cons: Slightly lower performance due to file system overhead. However, in the era of NVMe SSDs, this difference is negligible.

Advice: If you’re using a Cloud VPS like Vultr, DigitalOcean, or AWS, choose a Swap File to save administrative time.

Standard Swap File Deployment Process

The steps below apply to Ubuntu 20.04/22.04, Debian, and the RHEL family (CentOS, AlmaLinux).

Step 1: Check Resources

Determine if your server already has virtual memory using the command:

sudo swapon --show

If the output is empty, your server is running entirely on physical RAM. Use the free -h command to see the total available space.

Step 2: Initialize the File

To create 2GB of SWAP, the fallocate command is the fastest option as it allocates disk space immediately:

sudo fallocate -l 2G /swapfile

If the system reports that this is not supported, use the traditional dd command:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152

Step 3: Secure the SWAP File

The SWAP file contains temporary data from RAM, which may include sensitive information. You must restrict access to the root user only:

sudo chmod 600 /swapfile

If you skip this step, any user with server access could potentially inspect the data in virtual memory.

Step 4: Enable the System

Format the file and put it into use:

sudo mkswap /swapfile
sudo swapon /swapfile

Run free -h again, and you will see the Swap line displaying 2.0Gi.

Step 5: Make it Permanent

By default, SWAP is lost after a reboot. To have the system recognize it automatically at startup, add the configuration to /etc/fstab. First, create a backup to prevent errors:

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Optimizing the Swappiness Parameter

Many wonder why Linux uses SWAP even when RAM is still available, causing the system to slow down. The main culprit is the Swappiness value (ranging from 0-100).

  • Default (60): Linux will start using SWAP when about 40% of RAM is free.
  • Recommended (10-20): Only use SWAP when RAM is truly exhausted (around 10% remaining).

Change it to 10 immediately:

sudo sysctl vm.swappiness=10

To save this configuration, add the line vm.swappiness=10 to the end of the /etc/sysctl.conf file.

Real-world Experience

Here are 3 golden rules I’ve gathered:

  1. Sizing Formula: For RAM under 2GB, create SWAP at double the size (2x). For RAM over 4GB, creating SWAP equal to the RAM size (1x) is sufficient.
  2. I/O Wait Warning: If the %wa metric in the top command is frequently > 10%, it means the CPU is waiting too long for the disk to process SWAP. This is when you must upgrade physical RAM instead of relying on virtual memory.
  3. SSD Lifespan: Don’t worry too much about SWAP damaging your SSD. Modern SSDs have very high TBW (Total Bytes Written) ratings, capable of enduring SWAP write intensity for many years.

Configuring SWAP is a basic yet crucial technique for increasing server stability. Good luck with your system optimization!

Share: