Swap: Why You Need It and How to Enable It
Published on 2025-09-03
Swap is virtual memory on disk that saves your VPS from crashing when RAM runs out. On low-cost servers, it’s an indispensable tool to avoid failures during short-term memory usage spikes. Setup is simple: create a file, enable it, and add it to autoload. But remember — it’s insurance, not a replacement for RAM.
Introduction
Modern VPSs are usually provisioned with enough RAM but often without swap (swap file or partition). This is a deliberate choice by providers who want you to quickly upgrade to a more expensive plan.
As a result, when you run out of memory, the system starts behaving unpredictably, and in the worst case, the OOM Killer (Out of Memory Killer) is triggered, forcibly terminating processes.
On budget plans, where every megabyte counts, swap becomes an important tool. It won’t replace RAM, but it will save your server from crashing at moments when more memory is needed than physically available.
What is swap and how does it work
Swap is an area on your hard drive or SSD that Linux uses as additional RAM. When RAM is full, the kernel moves less frequently used data into swap, freeing space for active processes.
It’s useful for workloads with short memory spikes:
- Compilers (npm, gcc).
- Databases (MySQL, PostgreSQL) that temporarily cache large amounts of data.
- Background scripts and cron jobs.
It’s important to understand: disk access is orders of magnitude slower than RAM. Swap is insurance, not a way to speed up your system.
How to enable Swap on a VPS
Most providers (DigitalOcean, Vultr, etc.) don’t enable swap by default, but you can easily create it yourself using a file.
Create the file
sudo fallocate -l 1G /swapfileIf
fallocateis unavailable:sudo dd if=/dev/zero of=/swapfile bs=1M count=1024Restrict file permissions
sudo chmod 600 /swapfileInitialize and activate
sudo mkswap /swapfile sudo swapon /swapfileEnable at boot
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabAdjust swappiness Default = 60. For servers, set to 10 or lower:
sudo sysctl vm.swappiness=10To make it persistent: add
vm.swappiness=10to/etc/sysctl.conf. 💡 Additionally, to reduce aggressive inode/dentry cache cleanup:vm.vfs_cache_pressure=50
Verification
Check memory and swap:
free -h
Active swap partitions/files:
swapon --show
How to choose swap size
Recommendations vary:
- RAM ≤ 2 GB: RAM × 2
- 2–8 GB: RAM × 1
- 8–16 GB: 2–4 GB
- > 16 GB: 1–2 GB (or no swap)
When swap is not needed
Swap is not always useful:
- Servers with large amounts of RAM (32 GB+) — memory exhaustion is highly unlikely.
- Databases with fine-tuned configs — swap may degrade performance due to I/O.
- Kubernetes and containers — swap is usually disabled for predictability.
- Some providers (e.g., OpenVZ VPS or managed Kubernetes) — swap may be prohibited.
- ⚠ On SSD/NVMe, active swap accelerates disk wear.
Conclusion
Swap is insurance against the OOM Killer, not a way to speed up your server. With proper configuration, it helps you:
- Survive memory usage spikes.
- Reduce the risk of application crashes.
- Save on VPS upgrades.
But it should be used with an understanding of its limitations and only as a temporary solution.