Increase Swap in Linux

Swap space is fake memory that is actually disk space. The purpose of swap space is to be emergency overflow memory. Normally, if your server runs out of memory, the Linux kernel has to decide which processes to kill to free up memory. Often it’s MySQL that gets killed first.

You can view your existing Swap memory usage and details by the following commands.

free -m

Remember: The swap space shown from the above command does not mean your server is actively swapping at the moment; swap space is only used when your server runs out of memory. This number can be cleared by rebooting your server.

Increase or add Swap

Type the following commands one by one as root.

swapoff -a
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

That’s it. You are done.