server RAM memory

VPS Swap Memory: What It Is & When to Use It

June 2026 · 5 min read

A VPS with low RAM often stops working suddenly when memory is exhausted. Swap is an emergency solution that allows the system to keep running, but it comes with tradeoffs you need to understand.


What Is Swap?

Swap is disk space that Linux uses as an extension of RAM. When RAM is full, the system moves unused memory pages to swap storage and frees up RAM for active use. This allows the system to continue running even when physical memory is exhausted.

The main drawback: Disk is hundreds to thousands of times slower than RAM. When the system starts swapping heavily, the server becomes very slow, but it's still better than the OOM Killer immediately terminating processes.

Should You Have Swap?

ScenarioRecommendation
RAM 1 GB or lessShould have swap of at least 1–2 GB
RAM 2–4 GB1–2 GB swap helps handle memory spikes
RAM 8 GB and aboveNot necessary, but no harm if present
SSD/NVMeSwap on SSD is much faster than HDD; no downside to having it
Production databaseAvoid swap — latency spikes impact query time significantly

Creating a Swap File

# Create a 2GB swap file
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

# Verify
free -h
swapon --show

To make it persistent after reboot, add it to /etc/fstab:

/swapfile  none  swap  sw  0  0

Adjusting Swappiness

Swappiness is a kernel parameter that controls how quickly the system moves memory to swap. It ranges from 0–100, with a default of 60.

# Change temporarily
sysctl vm.swappiness=10

# Change permanently — add to /etc/sysctl.conf
vm.swappiness=10

Monitoring Swap Usage

# View swap statistics
vmstat -s | grep swap

# See which process uses the most swap
for pid in /proc/[0-9]*/status; do
  awk '/VmSwap/{printf "%s %s\n", FILENAME, $2" "$3}' $pid 2>/dev/null
done | sort -k3 -rn | head -10

Resizing an Existing Swap

If you need to expand swap or move it to a different file:

# Disable swap first
swapoff /swapfile

# Expand the size (example: increase to 4G)
fallocate -l 4G /swapfile
mkswap /swapfile
swapon /swapfile

# Verify
free -h
swapon --show
NVMe vs HDD: On VPS with NVMe SSD, swap performs much faster than on HDD. Latency can differ by 10–50x. If your disk is NVMe, having swap has minimal impact on overall performance.

Setting vm.vfs_cache_pressure

This parameter controls how aggressively the kernel frees memory used for directory/inode cache to make room for other uses:

# Default value = 100
# Below 100 = keep cache longer (good for file-heavy workloads)
# Above 100 = free cache faster (good when RAM is tight)
echo 'vm.vfs_cache_pressure=50' >> /etc/sysctl.conf
sysctl -p

Verify Swap is Working Correctly

After creating swap and setting swappiness, confirm that the kernel recognizes swap and is using the configured values. Also ensure swap mounts automatically after reboot:

# 1. Check if swap is activated
swapon --show
free -h        # Look at the Swap line — should not be 0

# 2. Verify current swappiness value
cat /proc/sys/vm/swappiness

# 3. Check that swap will mount automatically after reboot
grep swap /etc/fstab

# 4. See which process uses the most swap
for f in /proc/*/status; do awk '/VmSwap|Name/{printf $2" "$3}END{print ""}' $f; done | sort -k2 -n -r | head
Tip: If free -h shows swap constantly full while RAM is still available, it means swappiness is too high — reduce it to 10 on database/application servers. However, if swap is full "while RAM is also full" at the same time, that's a signal you need more RAM, not just more swap space.

Common Issues & Troubleshooting

Swap not activated after reboot despite being in /etc/fstab

Check the syntax in /etc/fstab and the permissions on the swapfile:

# Check if it's in fstab
grep swap /etc/fstab
# Should show: /swapfile  none  swap  sw  0  0

# Check file permissions
ls -la /swapfile
# Should be -rw------- (600) — if not, swap won't activate

chmod 600 /swapfile
swapon /swapfile

System swapping heavily while RAM is still available

This happens when swappiness is too high (default 60); the kernel moves pages to swap very aggressively. Lower it:

cat /proc/sys/vm/swappiness   # Check current value
sysctl vm.swappiness=10       # Change temporarily

# If it helps, add to /etc/sysctl.conf to persist
echo 'vm.swappiness=10' >> /etc/sysctl.conf

fallocate: fallocate failed: Operation not supported

This occurs on VPS with certain filesystem types like btrfs. Use dd instead:

dd if=/dev/zero of=/swapfile bs=1M count=2048   # 2GB
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

OOM Killer terminates processes even with swap available

This happens when swap is full or the process has a high oom_score_adj. Check the logs:

dmesg | grep -i "oom\|killed process"
# See which process was killed and how much swap is in use
free -h
swapon --show

Frequently Asked Questions

How much swap should a 1 GB VPS have?

2 GB is the recommended amount. It provides sufficient buffer for temporary memory spikes without consuming excessive disk space. If you have plenty of disk space and a heavy workload (such as compiling code), you could increase it to 4 GB.

Can I enable swap inside a Docker container?

It depends on the host and cgroup configuration. If your VPS uses KVM, swap works normally. On OpenVZ/LXC, some providers disable or limit swap. Verify with swapon --show after setup.

Does swap on SSD cause disk wear problems?

For VPS hosting, you don't own the disk, so don't worry. For a home server with heavy swap usage, there could be long-term SSD lifespan impact. However, most VPS providers use enterprise SSDs with very high TBW (total bytes written) ratings.

What's the difference between a swap partition and a swap file?

Swap partitions are slightly faster (direct disk block access, bypassing the filesystem), but harder to resize. Swap files are easier to resize and remove. On modern VPS with SSD, the performance difference is negligible.


Summary

Swap is not a long-term solution for low RAM, but it's a good safeguard against unexpected traffic spikes or unforeseen memory leaks that could crash your system. If you find yourself using swap regularly, that's a sign you should upgrade your RAM. For optimal performance, reduce swappiness to 10 so the system prioritizes RAM use before resorting to swap.