Adding swap
Create a swap file to protect small VPS plans from out-of-memory crashes.
1 min read
On this page
Swap is disk space used as overflow memory. It's slower than RAM, but it can stop the out-of-memory killer from terminating your apps during short spikes.
Create a 2 GB swap file#
bash
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileMake it permanent:
bash
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabCheck it#
bash
free -h
swapon --showTune swappiness#
Lower values make Linux prefer RAM:
bash
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.confRemove swap#
bash
sudo swapoff /swapfile
sudo rm /swapfile
# then delete the /swapfile line from /etc/fstabWas this page helpful?
