August 08, 2026 · by CLIQHOST
Every server administrator has encountered the dreaded Out of Memory (OOM) error at least once. Processes crash, applications stop responding, and the server becomes unusable. The quickest and most effective solution in these situations is configuring a swap file.
In this guide, you'll learn exactly what swap is, when and why you need it, and how to set it up step by step on a VPS SSD or VPS NVMe running popular Linux distributions like Ubuntu or Debian.
Swap is a storage area (either a dedicated partition or a file) that the operating system uses as an extension of physical RAM. When RAM is fully consumed, the Linux kernel moves the least-used memory pages to disk, freeing up space for active processes.
Key advantages:
- Prevents crashes caused by insufficient RAM
- Allows running demanding applications on a budget VPS plan
- Buys time for proper diagnosis and infrastructure scaling
Drawbacks to keep in mind:
- Disk storage (even SSD/NVMe) is slower than RAM
- Excessive swapping degrades application performance
- Swap is not a permanent replacement for insufficient RAM
Ground rule: Swap is a safety net, not a long-term solution. If your server constantly uses swap, it's time to upgrade your VPS plan.
Before configuring anything, check the current memory and swap status:
free -h
Example output:
total used free shared buff/cache available
Mem: 1.9Gi 800Mi 300Mi 20Mi 850Mi 1.0Gi
Swap: 0B 0B 0B
If the Swap line shows 0B, no swap is configured. You can also run:
swapon --show
If it returns nothing, proceed with the steps below.
General guidelines for modern systems:
| Available RAM | Recommended Swap |
|---|---|
| < 2 GB | 2× RAM (e.g., 4 GB) |
| 2–8 GB | Equal to RAM (4–8 GB) |
| 8–64 GB | At least 4 GB, max 8 GB |
| > 64 GB | Swap optional, situational |
For a VPS NVMe with 2 GB of RAM, a 2–4 GB swap file offers a great balance between protection and performance.
Use fallocate to quickly allocate space (or dd if fallocate is unavailable):
sudo fallocate -l 2G /swapfile
Or with dd:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Verify the file was created:
ls -lh /swapfile
The swap file must be accessible only by root, otherwise Linux will refuse to activate it:
sudo chmod 600 /swapfile
Verify:
ls -lh /swapfile
# -rw------- 1 root root 2.0G ...
sudo mkswap /swapfile
Expected output:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
sudo swapon /swapfile
Confirm it's active:
swapon --show
free -h
The Swap line should now display the configured size.
The activation above is temporary — swap disappears after a reboot. To make it permanent, add an entry to /etc/fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Verify the entry was added:
cat /etc/fstab | grep swap
From now on, every time your Linux VPS restarts, swap will be activated automatically.
Swappiness controls how aggressively the Linux kernel uses swap versus RAM. The default value is 60, on a scale of 0 to 100.
For a web server running on a VPS SSD, a value of 10 is recommended:
sudo sysctl vm.swappiness=10
To persist after reboot:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
The vfs_cache_pressure parameter controls how quickly the kernel releases inode and dentry caches:
sudo sysctl vm.vfs_cache_pressure=50
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
The default is 100. Lowering it to 50 keeps more data cached in memory — beneficial for busy web servers.
Periodic monitoring is essential to understand your server's memory behaviour:
# General memory and swap usage
free -h
# Processes sorted by RAM consumption
ps aux --sort=-%mem | head -10
# Swap statistics
vmstat -s | grep -i swap
# Continuous monitoring (updated every 2 seconds)
watch -n 2 'free -h && swapon --show'
If swap is consistently used above 50%, it's a clear sign your VPS needs more RAM. Browse the available VPS NVMe plans for a seamless upgrade.
If you've upgraded RAM or no longer need swap:
# Deactivate swap
sudo swapoff /swapfile
# Remove the entry from /etc/fstab (edit manually)
sudo nano /etc/fstab
# Delete the file
sudo rm /swapfile
On a VPS with SSD or NVMe storage (like the VPS SSD and VPS NVMe plans at CLIQHOST), swap performs significantly faster than on a traditional HDD. The reduced latency of solid-state storage makes memory paging far less punishing for running applications.
However, SSD write cycles are finite. That's why:
- Keep swappiness low (10–20)
- Use swap as a safety net, not a primary memory resource
- Monitor SSD wear with smartctl if you have bare-metal access
sudo chmod 600 /swapfile
Some filesystems (e.g., Btrfs) do not support fallocate for swap. Use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Check the syntax in /etc/fstab:
sudo mount -a
# If an error appears, correct the swap line in fstab
Configuring a swap file on a Linux VPS is one of the simplest and most effective ways to add a layer of stability to your server. With just a few commands, you protect your applications from OOM crashes and gain the breathing room needed to scale properly.
If you're running a production environment and notice swap being used frequently, the CLIQHOST team is available for consultation and a smooth migration to a higher-resource plan. Explore our VPS SSD and VPS NVMe plans — built for performance, reliability, and growth.
Real reviews from customers who trust CLIQHOST for performance, reliability and expert technical support.
"We moved our online shop from a foreign host and the difference is night and day — pages load instantly and support replies in minutes, in Romanian."
"Migrated 12 client sites to CLIQHOST. Free migration, zero downtime, and the cPanel setup is exactly what my team needed. Highly recommend."
"Our NVMe VPS handles traffic spikes without a sweat. Full root, local datacenter, and billing in MDL — everything we wanted from a provider."