Swap space extends your VPS’s effective memory by moving idle pages from RAM to disk when physical memory runs low. Without swap, an out-of-memory (OOM) situation can kill critical processes like MySQL, Nginx, or your web application. However, swap on slow storage can cripple performance. This guide covers modern swap strategies — swap files, swappiness tuning, and ZRAM compression — to keep your VPS stable and fast.
For a full breakdown of VPS plans with sufficient RAM for your workload, check our performance benchmarks.
Swap File vs. Swap Partition: Modern Best Practice
Historically, Linux used swap partitions — dedicated disk partitions for swap data. Modern distributions prefer swap files because they are easier to resize, don’t require repartitioning, and perform identically under the Linux kernel (since kernel 5.0, swap file performance is on par with swap partitions on all filesystems).
For cloud VPS environments running ext4 or XFS, a swap file is the recommended approach. If your VPS uses Btrfs or ZFS, you may still need a swap partition — check your distribution’s documentation.
How Much Swap Do You Need?
The old rule of thumb (“2× RAM”) was designed for servers with spinning HDDs and small memories. Modern VPS servers with SSDs or NVMe storage need less swap because they have more RAM. Use these updated recommendations:
| RAM Size | Recommended Swap | With ZRAM + Swap | Use Case |
|---|---|---|---|
| 512 MB | 1 GB | 768 MB | Micro instances, lightweight proxies |
| 1 GB | 1 GB | 512 MB | Small WordPress, API backends |
| 2 GB | 1 GB | 512 MB | Medium web apps, Docker hosts |
| 4 GB | 1–2 GB | 512 MB | Database servers, CI runners |
| 8 GB+ | 1–2 GB | 0–512 MB | Heavy workloads, production |
If your VPS has NVMe storage (10× faster than SATA SSD), you can rely on swap more aggressively. If you’re on HDD-based VPS, avoid swap entirely and compare specs on our site for SSD-based alternatives.
Creating a Swap File on Your Linux VPS
These commands create a 2 GB swap file on a standard Linux VPS (Ubuntu 22.04+, Debian 12+, Rocky Linux 9+):
# Create a 2GB swap file
sudo fallocate -l 2G /swapfile
# Restrict permissions (root only)
sudo chmod 600 /swapfile
# Format as swap
sudo mkswap /swapfile
# Enable swap immediately
sudo swapon /swapfile
# Make permanent across reboots
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Verify it's active
sudo swapon --show
free -h
Note on fallocate: On some filesystems (XFS on older kernels), fallocate creates a sparse file that can cause “swap area header corrupted” errors. If you encounter this, use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Swappiness: The Most Important Swap Performance Tuning
The vm.swappiness kernel parameter controls how aggressively Linux uses swap. It ranges from 0 (never swap unless absolutely necessary) to 100 (swap aggressively). The default is 60 — too high for most VPS workloads.
Recommended swappiness values by workload:
| Workload | Swappiness | Rationale |
|---|---|---|
| Database servers (MySQL, PostgreSQL) | 1–5 | Keep all data in RAM, swap only under OOM pressure |
| Web servers (Nginx, Apache) | 10 | Balance — swap rarely, keep cached files in RAM |
| General applications | 10–20 | Standard recommendation for VPS |
| Desktop/file server | 30–60 | Background processes can swap harmlessly |
| Memory testing / low-RAM VPS | 1 | Preserve every MB of RAM for active processes |
To set swappiness to 10 immediately and permanently:
# Apply immediately
sudo sysctl vm.swappiness=10
# Make permanent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
# Verify
sysctl vm.swappiness
Supercharging Swap with ZRAM
ZRAM creates a compressed block device in RAM that acts as swap space. Because compressed data takes less room, a 512 MB ZRAM device can hold 1.5–2 GB of uncompressed memory pages. This is faster than disk swap (no I/O) and more efficient than unused idle RAM.
On Ubuntu/Debian:
sudo apt install zram-tools
# Edit /etc/default/zramswap and set PERCENT=50 (uses 50% of RAM)
sudo systemctl restart zramswap
swapon --show # You'll see /dev/zram0 as swap
With ZRAM active, set swappiness to 100 so the kernel prefers the fast compressed RAM swap over disk swap. This is the optimal configuration for budget VPS instances with 1–2 GB RAM.
Monitoring Swap Usage in Real Time
Use these tools to track swap health:
free -h— Quick total/used/free swap overviewvmstat 5— Showssi(swap in) andso(swap out) per second. Consistently high values (100+ MB/s) mean you need more RAMtop -o %MEM— Identify which processes are consuming the most memorycat /proc/meminfo | grep -i swap— Detailed swap statistics includingSwapCached
If vmstat shows sustained swap activity (si/so over 50 MB/s for extended periods), your VPS is under memory pressure. Consider upgrading your plan or see our VPS comparison table for plans with more RAM.
Common Swap Pitfalls
- Swap on HDD: HDD-backed swap causes severe latency spikes. Avoid entirely.
- Too much swap: Oversized swap masks memory leaks. Fix the leak instead.
- Multiple swap devices: Linux prioritizes by priority value (
swapon -p). Set ZRAM higher than disk swap. - Swap on Btrfs: Requires
nodatacowattribute on the swap file and kernel 5.0+.




Leave a Reply
You must be logged in to post a comment.