Configure swap and optimize memory on a low-RAM VPS

Why a low-RAM VPS needs swap memory
Running a low-RAM VPS (1 GB or less) on a Linux host is a tightrope walk. A single runaway process, a PHP-FPM pool holding stale connections, a MySQL query without an index, or a Node.js app leaking memory, can trigger the Out-Of-Memory (OOM) killer, which terminates the offending process abruptly. You see a kernel log entry like Out of memory: Killed process 1234 (mysqld), and your application becomes unresponsive. This is where swap memory becomes your safety net. Swap is a reserved space on your NVMe SSD or disk that the kernel uses as an extension of physical RAM. When RAM fills up, the kernel moves inactive memory pages to swap, freeing RAM for active processes. On an NVMe VPS hosting plan, the NVMe SSD is fast enough that swap access delay is minimal. Without swap, a memory spike often crashes the service. With proper swap configuration combined with kernel memory tuning, you can keep a low-RAM VPS stable under load.
Prerequisites
- A Linux VPS running Ubuntu 24.04 LTS (commands work on Debian 12 and most systemd distros with minor changes).
- Root access or a sudo user.
- At least 1 GB of free disk space for the swap file (check with
df -h). - SSH access to your VPS.
Step 1, Check current memory and swap state
Before making any changes, assess the current condition. Run free -h and swapon --show. A fresh VPS often has no swap or only a small default swap. The free -h output shows total, used, and available RAM alongside swap. If swap shows 0B total, there is no swap configured. If no swap exists, you will create a swap file. Verify also the available disk space with df -h /. For a high-performance NVMe SSD VPS, the swap file should sit on the fastest storage available, usually the root partition.
free -h
swapon --show
df -h /Expected output of swapon --show if no swap exists: no output. With swap: NAME TYPE SIZE USED PRIO.
Step 2, Create a swap file
Using a swap file is more flexible than a swap partition because you can resize or remove it without repartitioning. Allocate a file of the desired size using fallocate (fast on ext4/xfs) or dd as fallback. The rule of thumb for swap size: For RAM ≤ 2 GB, swap = 2× RAM; for RAM 2-8 GB, swap = 1× RAM; for more than 8 GB, swap = half the RAM. For a 1 GB VPS, 2 GB swap is a safe starting point.
sudo fallocate -l 2G /swapfileIf fallocate fails (e.g., on some filesystems), use dd:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progressSet the correct permissions: sudo chmod 600 /swapfile, only root should read/write the swap file to prevent security issues. Format the file as swap space sudo mkswap /swapfile, then activate it sudo swapon /swapfile. Verify activation with sudo swapon --show.
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --showExpected output of swapon --show should now show: /swapfile file 2G 0B -2.
Step 3, Make swap permanent across reboots
Without an entry in /etc/fstab, the swap file disappears after a reboot. Add the following line to /etc/fstab. Use a UUID for the device or the path directly. The safest approach is to use the exact path:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabVerify the entry is correct: cat /etc/fstab | grep swap. Mistyping this file can prevent the system from booting. Always double-check.
Verify the entry is correct: cat /etc/fstab | grep swap should show the line you added. Mistyping this file can prevent the system from booting. Always double-check.
Step 4, Tune kernel swappiness for optimal behavior
The kernel parameter vm.swappiness (0-100) controls how aggressively the kernel moves pages from RAM to swap. Default on Ubuntu is 60, which is too high for a VPS: it begins swapping long before RAM is full, hurting responsiveness. A value of 10 tells the kernel to avoid swap until RAM is almost full. For a low-RAM VPS running services directly, set swappiness to 10. For a database server (MySQL/PostgreSQL), you might go lower (1-5).
sudo sysctl vm.swappiness=10Make it permanent by adding to /etc/sysctl.conf or a dedicated file /etc/sysctl.d/99-swap.conf:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.confApply the change: sudo sysctl -p. Verify: cat /proc/sys/vm/swappiness → should return 10.
Another relevant parameter is vm.vfs_cache_pressure (default 100). Lowering it to 50 tells the kernel to retain dentry and inode caches longer, which can help performance on low-RAM VPS by reducing disk metadata reads. Add vm.vfs_cache_pressure=50 to the same sysctl config.
Step 5, Configure swap priority when multiple swap devices exist
If you have multiple swap areas (e.g., a swap partition and a swap file), the kernel uses the one with the highest priority first. The pri value in swapon sets this. Our swap file gets a default priority of -2. If you want to favor the file over a partition (or vice versa), set explicitly using sudo swapon --priority 100 /swapfile. In /etc/fstab, add pri=100 to the options field: /swapfile none swap sw,pri=100 0 0. Verify priorities with swapon --show.
Step 6, Monitor swap usage and free memory
After enabling swap, monitor with free -h and htop (install htop if not present). The free command shows total, used, free, and available RAM, plus total used swap. The htop utility displays per-process memory and indicates swap usage. To see which process uses swap, run top -o %MEM (sorted by memory) or smem -s swap (requires smem package).
free -h
htopIf swap usage grows and never decreases, the system is under memory pressure. Reassess your application memory footprint, add more RAM, or tune the service configuration (e.g., reduce PHP-FPM pm.max_children).
Sizing table for swap on low-RAM VPS
| VPS RAM | Recommended swap size | Use case |
|---|---|---|
| 512 MB | 1 GB | Light web server, single site, static content |
| 1 GB | 2 GB | WordPress, Node.js app, Python API |
| 2 GB | 2 GB | Multiple sites, ecommerce, databases |
| 4 GB | 2 GB | Containerized workloads, moderate database usage |
Troubleshooting
Swap file not activating after reboot
Check /etc/fstab for syntax errors. Run sudo mount -a to test the fstab entries. If the mount fails, edit the file using sudo nano /etc/fstab and correct the line.
OOM killer still kills processes even with swap
Swap only postpones OOM if combined memory demand exceeds RAM + swap. Review dmesg | grep -i "killed process" to identify the offending process. Reduce memory limits: for MySQL, lower innodb_buffer_pool_size; for PHP-FPM, reduce pm.max_children; for Node.js, limit the heap with --max-old-space-size.
High swappiness causing sluggish behavior
If the system feels slow even with free RAM available, check swappiness: cat /proc/sys/vm/swappiness. Set it to a lower value (1-10) as described in Step 4. Also check if swap is being used unnecessarily: vmstat 1 shows so (swap out) and si (swap in) columns. Consistent >0 values indicate heavy swap activity.
FAQ
Is swap memory slower than RAM?
Yes, swap on an NVMe SSD is typically 3-5x slower than DDR4 RAM. Swap is a fallback mechanism, not a replacement for physical RAM.
Can I use a swap partition instead of a file?
Yes, but a swap file is more flexible, you can resize or remove it without repartitioning the disk.
Should I disable swap if I have enough RAM?
No. Keep a small swap (e.g., 1 GB) to handle rare memory spikes. Disabling swap entirely can cause OOM kills during unexpected loads.
What is the ideal swappiness value for a database server?
For MySQL/PostgreSQL, set vm.swappiness=1 to swap only when absolutely necessary, preserving cache.
Does swap wear out NVMe SSDs?
NVMe SSDs have high endurance. Normal swap usage on a VPS (a few hundred MB per day) has negligible wear.
How much swap should a 512 MB VPS have?
At least 1 GB. More can help, but ensure enough disk space remains for the OS and applications.
Related articles
- Harden SSH on a new VPS with keys, ports, fail2ban, and CrowdSec
- Self-host n8n for workflow automation on a VPS
- Set up a WireGuard VPN on your VPS in 10 minutes
- Self-host local LLMs on a VPS with Ollama


