Knowledgebase article

How to add swap space to your server

Swap is extra memory on disk that your server uses when its RAM is full. It's slower than real RAM, but it can stop your server from crashing during a sudden spike. It's a little safety cushion for your server's heart.

On Managed Servers, Romeo takes care of this. This guide is for Unmanaged Servers.

Check if you already have swap

free -h
swapon --show

If Swap shows 0B, there's none.

Create a 2 GB swap file

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

Keep it after reboots

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

Tune how eagerly it's used

A low value tells Linux to prefer RAM and use swap only when needed:

sysctl vm.swappiness=10
echo 'vm.swappiness=10' >> /etc/sysctl.conf

How much swap?

A rule of thumb: 1 to 2 GB for servers with up to 8 GB of RAM, a bit more for bigger ones.

If your server uses swap all the time, it needs more RAM. Swap is a cushion, not a cure. Consider upgrading your server (see How to upgrade your server's resources).

Was this answer helpful?

Related articles

Still stuck? Romeo's got you.
Our support team replies around the clock, real humans who know your server.