Help Docs Performance Server Optimization Swap Memory

Swap Memory

Linux swap memory explained: Its role, benefits, & risks. Learn to check usage (`vmstat`, `sar`), manage swappiness, and add a swap file.

What is swap memory?

Swap memory is virtual memory stored on your server’s disk. It acts as an extension of RAM by moving inactive memory pages from RAM to the swap area (either a swap partition or a swap file). This frees up RAM for active tasks and improves system performance. Swap is not just an emergency fallback, it’s a core feature of how Linux manages memory efficiently.

Why swap is useful?

Swap lets your server:

  • Free up RAM by moving idle or dormant processes to disk.
  • Cache more frequently accessed files in RAM for faster reads.
  • Prevent out-of-memory (OOM) errors by using disk space as overflow.
Note

If your server has dormant applications using lots of RAM, the kernel can move them into swap to make space for active processes or disk caching.

Why swap isn’t always cleared?

You might wonder why swap usage remains high even after RAM is freed. That’s normal.

The Linux kernel tracks how often swapped-out memory is accessed. If the data isn’t needed, it stays in swap to avoid wasting time reading it back into RAM. Only frequently used data is brought back.

When swap is a problem?

Swap becomes an issue when your server is constantly swapping data in and out, this is a sign of memory pressure.

How to check active swap usage?

Run the following command to see current swap activity:

vmstat 5

Look for high numbers under the si (swap in) and so (swap out) columns.

Use sar to check swap activity over time:

sar -W

Check the pswpin/s and pswpout/s values. High values suggest frequent swapping and potential performance problems.

Note

High swap usage with high disk I/O usually means your server doesn’t have enough RAM for current workloads.


Should you change swappiness?

Swappiness controls how aggressively the kernel uses swap. Lowering it can backfire.

  • Default value: 60
  • Common value: 30
  • Lower than 10: Risky, may lead to more frequent OOM errors

Keeping swappiness at 30 gives the system time to prepare and use swap before running out of RAM.

Warning

Lowering swappiness might cause your server to slow down or crash under memory load. Don’t rely on anecdotal fixes like restarting Apache; it only resets open connections, not the underlying memory issue.

How to add a swap file? (if none exists)


If your server doesn’t have swap enabled, you can create a 2GB swap file:

Step-by-Step Instructions
  1. Check if swap file exists:
ll /swapfile

If it exists, use a different name like swapfile2 or swapfile012345.

  1. Create and enable swap file:
dd if=/dev/zero of=/swapfile count=2048 bs=1MiB
mkswap /swapfile
chmod 600 /swapfile
swapon /swapfile

Make it permanent:

echo "/swapfile none swap sw 0 0" >> /etc/fstab

Your server now has 2GB of virtual memory to fall back on during high load.

Summary

Swap memory is a powerful tool for managing limited RAM on Linux servers. It’s not something to fear, but it does require monitoring. Keep an eye on swap activity, avoid unnecessary tweaks to swappiness, and add a swap file if your server needs more memory support.

Was this article helpful?