SWAP
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 size | Recommended SWAP size |
<= 4GB RAM | min. 2GB SWAP |
4GB – 16GB RAM | min. 4GB SWAP |
16GB – 64GB RAM | min. 8GB SWAP |
64GB – 256GB RAM | min. 16GB SWAP |