Performance12 min read•Updated November 2026

How to Configure Swap Space and Optimize Memory on a Linux VPS

Quick answer

To add swap on a Linux VPS, create a file with sudo fallocate -l 2G /swapfile, secure it with chmod 600, run mkswap and swapon, then add /swapfile none swap sw 0 0 to /etc/fstab. For servers, 1–4 GB of swap with vm.swappiness=10 is a good default: it absorbs short memory spikes and prevents sudden out-of-memory kills. If vmstat shows constant swapping, you need more RAM, not more swap.

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:

RAMSuggested swapReasoning
Up to 2 GBEqual to RAMSmall servers benefit most from a buffer
4–8 GB2 GBAbsorbs spikes during deploys, updates and backups
16–32 GB2–4 GBSafety net only
64 GB+4 GB or noneDepends 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_size or PostgreSQL's shared_buffers set 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 -Xmx explicitly 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_limit or deploy.resources.limits.memory in 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:

  • vmstat shows sustained swap-in and swap-out during normal traffic.
  • The "available" figure in free stays under roughly 10–15% at busy times.
  • The OOM killer appears in dmesg even 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.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — Intel28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthBusiness websites, small apps, scheduled jobs and botsOrder
VPS 2 — Intel416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,169/monthBusiness software, mid-size databases, several sites on one serverCheck stock
VPS 3 — Intel832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,249/monthERP/CRM stacks, heavier databases, multiple production appsCheck stock
VPS 4 — Intel1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,299/monthHigh-memory workloads and consolidated production serversCheck 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.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — AMD28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthWebsites, small APIs, bots, staging and VPN serversOrder
VPS 2 — AMD416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,299/monthWooCommerce, Node.js/Python backends, Docker Compose stacks, CI runnersOrder
VPS 3 — AMD832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,499/monthProduction databases, high-traffic sites, multi-container appsOrder
VPS 4 — AMD1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,899/monthLarge production workloads, SaaS platforms, analytics, in-memory cachesOrder

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?
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 SkyRainCloud 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.
Instant UPI • ₹649/mo
Fast KVM Cloud Provisioning