VPS SETUP
Jun 23/2026

Setting Up Swap Space on a VPS for Better Stability

6 min read
Author
CloudStick Team
DevOps Engineer
Share this article
Setting Up Swap Space on a VPS for Better Stability
CloudStick
Swap Space on a VPS

What Is Swap Space and When Do You Need It?

Swap space is disk storage that Linux uses as overflow memory. When RAM fills up — during a compilation run, a database backup, or a spike in PHP workers — the kernel moves the least recently used memory pages to the swap area on disk. Without swap, the kernel invokes the OOM (Out of Memory) killer, which terminates processes to free RAM, often choosing your web server or database.

Swap is not a replacement for adequate RAM. It is 5–10x slower than physical memory. But on a 1 GB or 2 GB VPS running WordPress or a Node.js application, a 1–2 GB swap file is the difference between surviving a traffic spike and losing your server to the OOM killer. Most cloud providers do not provision swap by default — you must configure it yourself.

PREREQUISITE
You need SSH access with sudo privileges on Ubuntu 24.04. Swap files work on all major cloud providers (DigitalOcean, Vultr, Hetzner, AWS). Do not use a swap file on SSD-only volumes with limited write cycles — a swap partition is preferable in that case.

Creating a 2 GB Swap File on Ubuntu 24.04

A swap file is a regular file on your root partition that Linux designates as virtual memory. The process is: allocate the file with fallocate, format it as a swap area with mkswap, then enable it with swapon.

# Allocate a 2 GB file (adjust size as needed)
sudo fallocate -l 2G /swapfile
# Restrict permissions — swap files must not be world-readable
sudo chmod 600 /swapfile
# Format as a swap area
sudo mkswap /swapfile
# Enable the swap file immediately
sudo swapon /swapfile
# Verify swap is active
sudo swapon --show
# Expected:
# NAME TYPE SIZE USED PRIO
# /swapfile file 2G 0B -2

For sizing: a general rule is to match your RAM size up to 2 GB, then use half of RAM for servers with more than 2 GB. A 1 GB VPS gets 1–2 GB swap. A 4 GB VPS can use 2 GB swap. Beyond 8 GB of physical RAM, swap is rarely needed unless you're doing large in-memory operations.

Tuning vm.swappiness for Web Server Workloads

The vm.swappiness kernel parameter controls how aggressively Linux moves pages to swap. The default value of 60 means Linux will start swapping when RAM is at 40% usage — too aggressive for a web server. Lower values tell the kernel to prefer keeping application data in RAM and only use swap as a last resort.

# Check the current swappiness value
cat /proc/sys/vm/swappiness
# Default: 60
# Set swappiness to 10 for this session (takes effect immediately)
sudo sysctl vm.swappiness=10
# Verify the change
cat /proc/sys/vm/swappiness
# Output: 10
TIP
Use vm.swappiness=10 for WordPress and PHP web servers. For database servers (MySQL, PostgreSQL), use vm.swappiness=1 — databases use their own buffer pools and benefit from keeping RAM free for that purpose.

Making Swap Permanent Across Reboots

The swapon command activates swap for the current session only. After a reboot, it will be gone unless you add the swap file to /etc/fstab and persist the vm.swappiness setting to /etc/sysctl.conf.

# Add swap file to /etc/fstab (auto-mount on boot)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Verify the fstab entry was added
cat /etc/fstab | grep swap
# Persist vm.swappiness setting
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
# Apply sysctl settings without rebooting
sudo sysctl -p
# Confirm everything is correct after a reboot
sudo reboot
free -h # Run this after reboot to confirm swap is active

Monitoring Swap and Memory Usage

If your server is consistently using swap, you have a memory problem that swap alone cannot solve. Swap buys time; it does not fix an undersized server. Monitor how much swap is in use and act before it becomes a performance bottleneck — heavy swap usage causes high disk I/O and significantly slower response times.

# Quick memory and swap summary
free -h
# Live memory usage (updates every 2 seconds)
vmstat 2 5
# Show which processes are using the most memory
ps aux --sort=-%mem | head -10
# Check swap usage in detail
swapon --show
# If consistently >50% swap used, upgrade your RAM plan

"Swap is a safety net, not a memory upgrade. If your server uses more than 50% of swap regularly, you need more RAM — no amount of tuning will change that."

Watching Memory and Swap Stats in CloudStick

CloudStick displays real-time memory and swap usage in the server stats panel — no need to SSH in every time you want to check. The dashboard shows RAM used, swap used, and disk I/O at a glance, updated every minute. When swap usage creeps up over several days, you can catch it before it causes downtime.

For servers added to CloudStick, the agent installation also configures swap if your server size warrants it — CloudStick checks for swap during onboarding and suggests configuration if none is found. This automated check prevents the common issue of new servers sitting without swap until they OOM under load.

Leave a comment
Full Name
Email Address
Message
On this page

We use cookies to improve your experience

CloudStick uses cookies to personalise content, analyse traffic and keep you signed in. Cookie Policy · Terms of Service

Manage cookies