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.
ServoMix 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 ServoMix 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 |
|---|---|---|---|---|---|---|---|
| SMX vCore XS (Intel Xeon) | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹270/mo | Business websites, small apps, scheduled jobs and bots | Order |
| SMX vCore S (Intel Xeon) | 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 |
| SMX vCore M (Intel Xeon) | 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 |
| SMX vCore L (Intel Xeon) | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,248/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 |
|---|---|---|---|---|---|---|---|
| SMX vPro XS (AMD EPYC) | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹420/mo | Websites, small APIs, bots, staging and VPN servers | Order |
| SMX vPro S (AMD EPYC) | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹560/mo | WooCommerce, Node.js/Python backends, Docker Compose stacks, CI runners | Order |
| SMX vPro M (AMD EPYC) | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹840/mo | Production databases, high-traffic sites, multi-container apps | Order |
| SMX vPro L (AMD EPYC) | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹1,120/mo | Large production workloads, SaaS platforms, analytics, in-memory caches | Order |
Prices are monthly in INR as listed on the ServoMix 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.Does swap wear out NVMe SSDs?
Should I use a swap file or a swap partition?
Can I disable swap for databases?
Why does free show almost no free memory?
Which ServoMix plan should I choose if I keep running out of memory?
Related Guides
Frequently Asked Questions
Do I need swap on a VPS?
It is recommended. A small swap file (1–4 GB) absorbs short memory spikes and gives you time to react before the OOM killer terminates a process. It is a safety net, not extra RAM.
How much swap should a server with 8 GB of RAM have?
About 2 GB is a good default for an 8 GB VPS. More rarely helps: if a workload needs much more than the RAM you have, the server will thrash and become slow regardless of swap size.
What is a good vm.swappiness value for a server?
10 is a common choice for servers. It keeps application memory in RAM and uses swap mainly under pressure. The default of 60 is tuned for general-purpose systems.
Is swap usage bad?
Not by itself. Idle pages sitting in swap are harmless. What hurts performance is constant swap activity, visible as sustained non-zero si and so values in vmstat .
How do I check if the OOM killer stopped my application?
Run 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.
Does swap wear out NVMe SSDs?
With low swappiness and occasional use, the extra writes are negligible compared to normal database and log activity. A server that swaps constantly needs more RAM anyway.
Should I use a swap file or a swap partition?
On a VPS, a swap file is easier: you can create, resize or remove it without repartitioning, and performance on modern kernels is effectively the same.
Can I disable swap for databases?
Some database operators run without swap to avoid unpredictable latency, but then they must size memory carefully because there is no buffer before the OOM killer acts. For most small and medium servers, a small swap file with low swappiness is the safer default.
Why does free show almost no free memory?
Linux uses spare RAM as file cache to speed up disk access and releases it when applications need it. Read the available column instead of free.
Which ServoMix plan should I choose if I keep running out of memory?
Move up one RAM tier: 8 GB to 16 GB, 16 GB to 32 GB, and so on. If the workload is a database or many containers, AMD EPYC and Intel Xeon plans offer the same RAM tiers, so choose the CPU family that suits the rest of your workload.
Your feedback helps us continuously improve ServoMix guides.
Know someone who may find this guide useful?
