When a Linux server runs out of memory, the kernel's out-of-memory (OOM) killer terminates a process, often your database or application. Swap space gives the kernel somewhere to move rarely used memory pages so that a short spike does not crash anything. Configured well, swap makes a VPS more resilient. Configured badly, or used to paper over a server that is simply too small, it makes everything slow.
This guide covers checking current memory, adding a swap file, choosing a size, tuning swappiness and, most importantly, telling the difference between healthy and unhealthy swap use.
Step 1: Check Memory and Existing Swap
free -h
swapon --show
cat /proc/sys/vm/swappiness
In free -h, focus on the available column, not "free". Linux deliberately uses idle RAM for the file cache (shown under buff/cache) and gives it back instantly when applications need it. Low "free" memory is normal; low "available" memory is the warning sign.
If swapon --show prints nothing, the server has no swap. Many VPS images ship without it.
Step 2: Decide How Much Swap
Old rules like "twice the RAM" came from desktop systems with little memory and hibernation. Servers do not hibernate, and huge swap only lets a runaway process thrash for longer before failing. Practical guidance:
| RAM | Suggested swap | Reasoning |
|---|---|---|
| Up to 2 GB | Equal to RAM | Small servers benefit most from a buffer |
| 4–8 GB | 2 GB | Absorbs spikes during deploys, updates and backups |
| 16–32 GB | 2–4 GB | Safety net only |
| 64 GB+ | 4 GB or none | Depends on workload; some databases prefer no swap |
Check free disk first with df -h /. Swap uses disk space, and on a 40 GB disk you do not want to give away 8 GB.
Step 3: Create and Enable a Swap File
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
The chmod 600 step matters: swap can contain fragments of memory such as passwords and keys, so only root may read it. If fallocate fails on your filesystem, create the file with dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
On Btrfs, swap files need extra steps (no copy-on-write, no compression). The default ext4 used by most VPS images has no such restrictions.
Step 4: Make It Permanent
Back up /etc/fstab, then add the swap file so it is enabled at boot:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo findmnt --verify
findmnt --verify checks fstab for mistakes. An error in fstab can stop a server from booting cleanly, so always verify before rebooting.
Step 5: Tune Swappiness and Cache Pressure
vm.swappiness (0–200, default 60 on most distributions) controls how willing the kernel is to swap application memory versus dropping file cache. For servers, a low value keeps application memory in RAM and uses swap mainly as an emergency reserve:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl --system
Setting it to 0 does not disable swap. It only tells the kernel to avoid it until memory is very tight. Leave vm.vfs_cache_pressure at its default unless you have measured a reason to change it.
Healthy vs Unhealthy Swap Use
Seeing some swap "used" is not a problem. The kernel may have moved idle pages there days ago. What matters is activity: pages constantly moving in and out. Watch it with vmstat:
vmstat 2 10
Look at the si (swap in) and so (swap out) columns. Occasional non-zero values are fine. Sustained values in the hundreds or thousands mean the server is thrashing: it spends its time moving memory to disk instead of doing work, latency jumps and the CPU wa (I/O wait) column rises. Swap cannot fix that. You must reduce memory usage or add RAM.
Find What Is Using Memory
ps aux --sort=-%mem | head -n 10
sudo smem -rs swap | head # per-process swap use (apt install smem)
sudo dmesg -T | grep -i -E "killed process|out of memory"
The last command shows whether the OOM killer has already terminated something, and which process. Common causes and fixes:
- Databases with oversized buffers: MySQL's
innodb_buffer_pool_sizeor PostgreSQL'sshared_buffersset for a bigger server. Size them for the RAM you actually have. - Too many app workers: PHP-FPM
pm.max_children, Gunicorn workers or PM2 cluster instances multiplied by per-process memory can exceed RAM. Calculate: workers × typical worker memory should leave headroom. - Java heaps: set
-Xmxexplicitly instead of letting the JVM size itself. - Memory leaks: a process whose memory grows steadily over days needs fixing; a scheduled restart is only a stopgap.
- Containers without limits: set
mem_limitordeploy.resources.limits.memoryin Docker Compose so one container cannot starve the rest.
What About zram?
zram creates compressed swap in RAM. It is useful on very small machines, because it trades CPU time for effective memory, but on a production VPS a small disk swap file plus correctly sized services is simpler and more predictable. Consider zram only after measuring a benefit.
Resizing or Removing Swap
sudo swapoff /swapfile
sudo fallocate -l 4G /swapfile # or rm /swapfile and delete the fstab line
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapoff must move swapped pages back into RAM first, so make sure enough memory is available or it will fail.
When to Add RAM Instead
Upgrade memory when any of these is true:
vmstatshows sustained swap-in and swap-out during normal traffic.- The "available" figure in
freestays under roughly 10–15% at busy times. - The OOM killer appears in
dmesgeven after you have right-sized buffers and worker counts. - Response times rise whenever a backup, deploy or cron job runs.
Our VPS size calculator estimates RAM for common stacks such as WordPress, Docker and databases.
SkyRainCloud Plans for This Workload
Swap is a safety net, not a substitute for RAM. If your server swaps continuously, move to a plan with more memory: each SkyRainCloud VPS step doubles RAM, from 8 GB to 16, 32 and 64 GB.
Intel Xeon Platinum 8269CY VPS
KVM VPS on Intel Xeon Platinum 8269CY (up to 3.80 GHz Turbo) with DDR4 ECC memory, NVMe SSD, full root access and an unmetered 1 Gbps port.
| Plan | vCores | RAM | Storage | Network | Price | Good for | Buy |
|---|---|---|---|---|---|---|---|
| VPS 1 — Intel | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹649/month | Business websites, small apps, scheduled jobs and bots | Order |
| VPS 2 — Intel | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹1,169/month | Business software, mid-size databases, several sites on one server | Check stock |
| VPS 3 — Intel | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹2,249/month | ERP/CRM stacks, heavier databases, multiple production apps | Check stock |
| VPS 4 — Intel | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,299/month | High-memory workloads and consolidated production servers | Check stock |
AMD EPYC 7C13 VPS
Dedicated KVM vCores on AMD EPYC 7C13 (up to 3.70 GHz) with DDR4 ECC memory, NVMe SSD and an unmetered 1 Gbps port.
| Plan | vCores | RAM | Storage | Network | Price | Good for | Buy |
|---|---|---|---|---|---|---|---|
| VPS 1 — AMD | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹649/month | Websites, small APIs, bots, staging and VPN servers | Order |
| VPS 2 — AMD | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹1,299/month | WooCommerce, Node.js/Python backends, Docker Compose stacks, CI runners | Order |
| VPS 3 — AMD | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹2,499/month | Production databases, high-traffic sites, multi-container apps | Order |
| VPS 4 — AMD | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,899/month | Large production workloads, SaaS platforms, analytics, in-memory caches | Order |
Prices are monthly in INR as listed on the SkyRainCloud store (verified 2026-11-14). Stock and final pricing are shown on the product page at checkout.
Frequently Asked Questions
Do I need swap on a VPS?
How much swap should a server with 8 GB of RAM have?
What is a good vm.swappiness value for a server?
Is swap usage bad?
si and so values in vmstat.How do I check if the OOM killer stopped my application?
sudo dmesg -T | grep -i -E "killed process|out of memory" or journalctl -k | grep -i oom. It shows which process was killed and when.