Eric Range
SWAP
Range Eric
Nov 25, 2019
Technical director of Enginsight

When memory is running low, you not only risk inconsistent memory areas, but also a system crash that can lead to data loss.

The SWAP is an extended memory area on the hard disk that can be used in an emergency to ensure operation.

To create a SWAP area, simply copy and execute the following commands.

Debian / Ubuntu

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
echo 'vm.vfs_cache_pressure=10' | sudo tee -a /etc/sysctl.conf

CentOS, Redhat

sudo dd if=/dev/zero of=/swapfile count=2048 bs=1MiB
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile swap swap sw 0 0' | sudo tee -a /etc/fstab
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
echo 'vm.vfs_cache_pressure=10' | sudo tee -a /etc/sysctl.conf

Recommended SWAP size

Recommended RAM sizeRecommended SWAP size
<= 4GB RAMmin. 2GB SWAP
4GB – 16GB RAMmin. 4GB SWAP
16GB – 64GB RAMmin. 8GB SWAP
64GB – 256GB RAMmin. 16GB SWAP