Linux, Web Server & Database
Configure Linux Swap File
Set up a swap file so your server doesn't crash or kill processes when RAM runs out.
The Problem
Your server runs out of RAM under load and either crashes, freezes, or the kernel starts killing processes seemingly at random — and many budget VPS plans ship with no swap configured at all by default.
About this problem
Swap acts as an overflow for RAM, letting the system page less-active memory out to disk instead of running out entirely — it's not a substitute for having enough RAM for sustained heavy use, but it smooths over short memory spikes that would otherwise crash a service or trigger the kernel's out-of-memory killer.
This comes up most on small VPS plans (1-2GB RAM) running anything memory-hungry, like a CMS with several plugins, a build process, or a database alongside a web server.
What's Included
- Creating a swap file sized appropriately for your VPS's RAM and workload
- Setting correct permissions and enabling it immediately
- Making the swap file persistent across reboots via fstab
- Tuning swappiness to a sensible value for a server workload
What's NOT Included
- Fixing the underlying cause of excessive memory use (a separate diagnostic if the real issue is a memory leak)
- Upgrading your VPS's actual RAM — that's a hosting plan decision
- Setting up a swap partition instead of a file (rarely necessary on modern VPS setups)
How It Works
- Check current memory and existing swap status to confirm none is already configured.
- Create a swap file of an appropriate size (commonly matching or exceeding RAM on smaller VPS plans) using fallocate or dd.
- Set strict permissions on the file and format it as swap with mkswap.
- Enable it immediately with swapon and add an entry to /etc/fstab so it persists across reboots.
- Tune vm.swappiness to a lower value appropriate for a server (rather than desktop) workload.
In practice: you buy the service, send over whatever access or details the job needs, I investigate and do the work, and you confirm it's resolved before we call it done.
Frequently Asked Questions
- How much swap space do I actually need?
- A common starting point is swap equal to your RAM on servers with 2GB or less, tapering down proportionally on servers with more RAM, though the right amount depends on your actual workload.
- Does swap slow down my server?
- Accessing swap is slower than RAM since it lives on disk, so heavy sustained swapping does hurt performance — but as a safety net for occasional spikes, it's far better than a crash.
- Why doesn't my VPS have swap by default?
- Many cloud VPS images ship with no swap configured at all, leaving it to the user to set up — it's not an oversight, just not a universal default across providers.
- What is swappiness and what should it be set to?
- Swappiness controls how aggressively the kernel moves memory to swap; a lower value (e.g. 10) is generally recommended for servers to prefer RAM and only use swap when genuinely needed.
- Can I resize a swap file after creating it?
- Not directly — it typically needs to be disabled, removed, recreated at the new size, and re-enabled, rather than resized in place.
- Will adding swap fix my out-of-memory crashes permanently?
- It helps absorb short-term spikes, but if a process is genuinely leaking memory or your workload has simply outgrown the server's RAM, swap only delays the same problem rather than solving it.