Skip to main content

Configuring SWAP on a Linux VPS: Extending RAM

VDS / VPS · 25.05.2025 · 5 min read
Illustration for “Configuring SWAP on a Linux VPS: Extending RAM”

SWAP is an area on disk where the Linux kernel offloads memory pages when there is not enough physical RAM. On a VDS with a small amount of memory, swap keeps processes from being killed by the OOM killer, but it pays for that with speed: disk is thousands of times slower than RAM.

Creating a swap file

# Create a 2 GB file
fallocate -l 2G /swapfile

# Set the permissions — the kernel will refuse to use a swap file with open permissions
chmod 600 /swapfile

# Format it as swap
mkswap /swapfile

# Activate it
swapon /swapfile

# Check
swapon --show
free -h

If fallocate is unavailable (rare, on some filesystems like btrfs with compression), the file can be created more slowly but reliably with dd if=/dev/zero of=/swapfile bs=1M count=2048.

Mounting swap automatically at boot

# Add to /etc/fstab
/swapfile  none  swap  sw  0  0

Without this line, the swap file is only active until the next reboot — after a server restart, free -h will show zero in the Swap column again, and you will have to run swapon manually each time.

Tuning swappiness

# Check the current value (0-100)
cat /proc/sys/vm/swappiness  # Default: 60

# Change it (10 = use swap only when memory is really tight)
sysctl vm.swappiness=10

# Make it permanent
echo "vm.swappiness=10" >> /etc/sysctl.conf

The default value of 60 is tuned for desktops, where UI responsiveness matters more than server throughput. On a VDS running a database or a web server, a lower value (10–20) delays moving to swap until memory is genuinely short, instead of doing it at the first opportunity to free up a bit of RAM for the file cache.

Common Errors Explained

The error swapon: /swapfile: insecure permissions 0644, 0600 suggested is the kernel warning that the file's permissions are too open: without chmod 600, an unprivileged user could in theory read the swap contents, and that can hold anything from other processes' memory, including passwords.

swapon: /swapfile: swapon failed: Invalid argument

This error usually means the file was not formatted with mkswap before swapon, or the filesystem underneath it does not support swap files in its current configuration (for example, some network filesystems or btrfs without special preparation).

Swap filling up while RAM stays free is a sign not of a memory shortage, but of a high swappiness combined with rarely used processes that have been sitting in memory for a long time. The kernel may push them into swap preemptively to free up RAM for cache, even when there is no immediate need to. Check this with vmstat 1: the si/so (swap in/out) columns should sit close to zero when the system is idle — sustained non-zero activity is already a warning sign.

Zswap as an Alternative to a Plain Swap File

Zswap is a kernel layer that compresses pages before swapping them out and keeps some of them compressed right in RAM, only touching disk once the compressed pool fills up. On a VDS with an SSD this reduces the number of actual disk writes under moderate swapping:

# Enable zswap on the fly (until the next reboot)
echo 1 > /sys/module/zswap/parameters/enabled

# Permanently — via a kernel parameter in GRUB
# add zswap.enabled=1 to GRUB_CMDLINE_LINUX
update-grub

Zswap does not replace a swap file entirely — it still needs a regular swap area as a fallback layer, it just reduces how often that layer is touched. For a VDS with 1–2 GB of RAM and no NVMe disk, this usually gives a noticeable improvement in responsiveness under short-lived memory spikes.

SWAP Size Recommendations

Server RAMSWAP sizeUse case
Up to 2 GB2x RAMBuffer when memory runs short
2–8 GB= RAMStandard configuration
8–32 GB4–8 GBFor emergencies only
Over 32 GB2–4 GBFor OOM killer dumps only
⚠️ SWAP on an HDD slows the server down dramatically. On an SSD it is acceptable, but active swapping (constant writes and reads) is a sign that there is not enough RAM.

When Swap Does Not Solve the Problem

  • The database's working set is consistently larger than RAM plus swap — the server will keep swapping heavily under load, and the right fix here is not more swap but a memory upgrade or query optimization.
  • The disk is network storage with high latency — swap on such a device turns any access to a swapped-out page into an operation with tens of milliseconds of delay, which feels like a freeze for interactive services.
  • A process is regularly killed by the OOM killer before swap even has time to fill up — that means a spike in memory usage that is too sudden, not too slow, and the fix here is capping the process's own memory (for example via systemd's MemoryMax), not a bigger swap.

Verifying the Result

  1. Confirm swap is visible after a reboot, not just right after a manual swapon: restart the server and run free -h again.
  2. Check the priority and actual usage: swapon --show shows how full it is in the Used column.
  3. Compare the vm.swappiness value before and after editing /etc/sysctl.conf: sysctl vm.swappiness should return the new value even after a reboot, not just in the current session.
  4. Watch vmstat 1 10 under normal load — sustained non-zero si/so means memory is still tight, and the swap file only softens the symptom rather than fixing the root cause covered in the article on Linux performance profiling.
Was this article helpful?
← Back to Knowledge Base Ask Support