Swap memory on VPS plans is disk space your server treats as extra RAM. When physical memory fills up, Linux moves pages of data that are not in active use from RAM to a swap file or swap partition on disk, then reads them back when something needs them again.
That sounds like free RAM, and it is not. Disk is far slower than memory, so anything parked in swap runs slowly until it returns to RAM. Swap still earns its place: it keeps a VPS alive through short memory spikes instead of letting the kernel kill a process.
How Swap Memory on VPS Works
Linux manages memory in fixed-size chunks called pages. When RAM gets tight, the kernel writes pages that have not been touched in a while to swap, freeing RAM for work that needs it right now. When a swapped-out page is needed again, the kernel reads it back from disk. This round trip is called paging, or swapping.
Linux swap memory can live in two places: a dedicated swap partition created at install time, or a swap file on the regular filesystem. Most VPS owners use a swap file, because you can create, resize, or remove it without repartitioning a disk.
Check the current state with two commands. free -h shows total, used, and free RAM alongside swap usage, and swapon --show lists each active swap device or file. If both show zero swap, your VPS has none configured.
Swap vs RAM: Why the Speed Gap Matters
The whole swap vs RAM question comes down to speed. RAM answers in nanoseconds; even a fast NVMe drive answers in microseconds, and a spinning disk is slower still. A process running from swap feels sluggish, and a server swapping constantly can feel frozen while the CPU idles, waiting on disk.
| RAM | Swap | |
|---|---|---|
| Where it lives | Memory chips | Disk (SSD, NVMe, or HDD) |
| Access speed | Very fast | Much slower than RAM |
| Best use | Active data and running processes | Idle pages and short overflow |
| Cost of use | None while it fits | Disk I/O on every page in or out |
| When it hurts | Running out triggers the OOM killer | Heavy use causes thrashing |
When Your VPS Needs Swap
A 1 GB or 2 GB VPS running a web server, PHP, and a database at the same time has very little headroom. A backup job, a traffic spike, or a plugin update can push memory use past the limit for a minute or two. With swap configured, the server slows down briefly and recovers. Without it, the kernel’s out-of-memory killer picks a process and ends it, which can mean a dead database or a crashed web server.
That safety net is the strongest argument for enabling swap on a small VPS. Our guide to when to enable swap on a VPS and how much to allocate walks through the decision, and the companion tutorial on how to create a swap file on a low-RAM VPS covers the commands.
Swap also helps on servers with bursty workloads. A VPS that idles at 40 percent memory most of the day but spikes during a nightly backup does not need a permanent RAM upgrade for a five-minute peak. Swap absorbs the peak, then the kernel pages data back as memory frees up.
When Swap Will Not Save You
Swap becomes a problem when a server leans on it all the time. If your VPS swaps heavily for hours, the disk cannot keep up, response times climb, and the server can end up slower than if it had simply run out of memory. Administrators call this thrashing, and the fix is not more swap. It is less memory demand or more RAM.
The usual suspects are easy to name: a database with a buffer pool sized for a bigger server, a cache layer holding more data than the VPS can afford, or a WordPress site with too many plugins. On a small VPS, tuning MariaDB for 1-2 GB of RAM often frees more memory than any amount of swap, and a WordPress allowed memory size error usually means a PHP limit or a plugin needs attention, not that swap is missing.
Swap also cannot fix a memory leak. A growing process will fill RAM, then swap, just more slowly. If swap use climbs steadily over days and never falls, find the leak instead of adding swap.
How Much Swap Should You Allocate?
There is no single correct size, but a common rule of thumb works for most VPS setups. The right amount of VPS swap space is roughly equal to RAM on small servers, tapering off as RAM grows, because a big server rarely needs a huge overflow area.
| VPS RAM | Suggested swap | Notes |
|---|---|---|
| 512 MB – 1 GB | 1 – 2 GB | Small servers benefit most from a safety net |
| 2 GB | 2 GB | Enough to absorb spikes from a LAMP or LEMP stack |
| 4 GB | 2 – 4 GB | Useful for backups and traffic peaks |
| 8 GB or more | 2 GB | A small buffer for emergencies is usually enough |
These are starting points, not rules. A server running a database with a large buffer pool should keep swap modest and swappiness low. If you are unsure, start with 2 GB, watch usage for a week, and adjust.
Swap File vs Swap Partition: Which Should You Use?
A swap partition is a slice of disk set aside at install time. It works on systems that cannot use swap files. The downside is rigidity: changing its size means repartitioning, which most VPS owners cannot do without rebuilding the server.
A swap file is a regular file on your existing filesystem. Setting up a swap file on VPS hosting takes minutes: create the file with fallocate, secure it, and turn it on with swapon. You can run more than one, which makes it easy to grow swap in steps. On modern kernels the performance difference between a file and a partition is small. For almost every VPS, a swap file is the practical choice.
Checking Swap Usage and Tuning Swappiness
Two numbers tell you whether swap is doing its job: how much swap is used, and how often pages move in and out. free -h shows the first. vmstat 1 shows the second in its si and so columns, swap in and swap out per second. Occasional activity during a backup is normal; constant activity means the server is short on RAM.
Swappiness controls how eagerly the kernel moves pages to swap. It is a number from 0 to 100, and the default on most distributions is 60. A lower value tells the kernel to prefer keeping data in RAM. Check the current value with cat /proc/sys/vm/swappiness and set it with sysctl vm.swappiness=10. To make the change survive a reboot, add vm.swappiness=10 to /etc/sysctl.conf.
A value around 10 suits most web and database servers. If your server runs a memory-hungry cache, it is also worth reducing Redis memory usage on a low-RAM VPS before reaching for more swap.
Common Swap Mistakes
The most common mistake is treating swap as a RAM upgrade. Adding 4 GB of swap to a 1 GB VPS does not make it a 5 GB server. It makes it a 1 GB server that slows to disk speed when it overflows. Swap buys time, not capacity.
The second mistake is putting swap on slow storage and then swapping heavily. A swap file on an old spinning disk will bottleneck long before the CPU does. If your VPS still uses HDD storage, keep swap small and fix the memory pressure instead.
The third is setting swappiness too high on a database server. With swappiness at 60 or above, the kernel may page out buffer pages the database needs again seconds later, turning fast queries into disk reads. Set it low and let the database keep its memory.
The last mistake is forgetting swap exists after upgrading RAM. A VPS that grew from 1 GB to 8 GB rarely needs the 2 GB swap file it was given at birth. Recheck swap size and swappiness whenever the plan changes.
Frequently Asked Questions
Does swap slow down my VPS?
Only when it is actively used. Idle swap costs nothing. Heavy paging is a signal to add RAM or reduce memory demand.
Should I disable swap if I have plenty of RAM?
There is no need to. A small swap file costs little disk space and gives the kernel somewhere to put idle pages. Disable it only if a specific application forbids swap.
Is a swap file as good as a swap partition?
For a VPS, yes. A swap file is easier to create, resize, and remove.
Swap memory on VPS servers is a safety net, not a substitute for RAM. Set up a modest swap file, keep swappiness low, and treat heavy swapping as a message: the server needs more memory or less work.