LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

What Happens When You Run a VPS with Zero Swap?

Linux OOM KillerFor budget-conscious server admins, optimizing every MB of RAM and disk is second nature. But one setting that often flies under the radar—or gets intentionally left out—is swap space. You might ask: What happens if I just don’t have any swap at all? Do I really need it?

The answers are:

  • Maybe nothing.  Maybe disaster.
  • It depends.

Why Would I Want a Flea Market, Anyway?

Swap space is disk-based virtual memory.  That’s a fancy way of saying that when your VPS runs out of memory, the kernel will “swap out” some of the RAM you’re using to disk, freeing up memory.

For example, let’s say you’re on a 512MB VPS and you’re using 490MB.  Now a new process starts which need 32MB.  490 + 32 = 522MB, which is more than what could fit it memory.  The kernel will go through your RAM and find 10MB of RAM that hasn’t been accessed in a while and “swap” it out to disk.

That’s great – now your 512MB VPS can use 522MB.  The only issue is that if some process needs to access that 10MB of RAM, it’s glacially slow.  Instead of accessing at RAM speeds, you’re accessing at disk speeds.  RAM speed is 10 to 100 nanoseconds.  NVMe speed is 10 to 100 microseconds – 100x to 1000x slower.  If you have SATA HDDs, it could be as much as 150,000x slower!

Usually, there are some background processes that are rarely accessed.  In my experience, using a little swap is fine, but if you start to really lean into it, your system starts to crawl.

In theory you could have 32MB of RAM and 20TB of disk and have 20TB of virtual RAM.  In practice, that would never work.  Your kernel would spend all its time swapping with no time to do any actual work.  That’s obviously an extreme example, but in general once you get to 2x RAM you’re probably getting in the danger zone.

Setting Up Swap

Often your server will come with swap pre-installed from whatever template the provider is using.  Either there will be a dedicated swap partition:

# swapon -s
Filename     Type          Size     Used      Priority
/dev/md0     partition 16759804   303884            -2

Or there will be a swapfile:

# swapon -s
Filename     Type       Size      Used      Priority
/swapfile    file    2457596     20340            -2

If you have no swap, it’s easy to add:

# fallocate -l 1G /swapfile
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile

Then add it to /etc/fstab for persistence:

/swapfile none swap sw 0 0

So What Happens If You Have No Swap?

When you run a VPS with zero swap, here’s what you can expect.

If your VPS starts to exceed its available memory, the OOM (Out Of Memory) killer will start terminating processes to free up memory.  There is a complex formula, that factors in how much memory a process is using, what its nice (priority) level is, and any custom adjustments you’ve added (in /proc/pid/oom_score_adj).

You can see a particular process’s oom score by looking at /proc/pid/oom_status.  Lower scores mean a process is less likely to be killed.  For example, on a Debian 12 server I have, the Nginx master process’s OOM score is 666.whereas my rtorrent process is 674.  If push comes to shove, rtorrent will be killed first.

You can protect a process by doing something like this:

echo -1000 > /proc/pid/oom_score_adj

Or make it a higher priority for killing with this:

echo 1000 > /proc/pid/oom_score_adj

But really, if you’re at the point where you need to worry about OOM scores, you need more RAM or swap.

Running Without Swap

VPSes or servers without swap run just fine…until they don’t.  Really what you’re losing is the cushion for bursting or unexpected load.  And that may be just fine.  If you’re running a small VPN server, for example, your memory footprint is extremely predictable.  For a webserver handling a growing web site with variable demand, having some cushion would be very handy.

No Comments

    Leave a Reply

    Some notes on commenting on LowEndBox:

    • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
    • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
    • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

    Your email address will not be published. Required fields are marked *