How to add a swap file on Linux?
Create swap file:
sudo dd if=/dev/zero of=/var/1GB.swap bs=1024 count=1048576
sudo chmod 600 1GB.swap
sudo chmod 600 1GB.swap
Create swap FS:
sudo mkswap /var/1GB.swap
Enable new file (active till reboot without adding to fstab):
sudo swapon /var/1GB.swap
swapon --show
swapon --show
Add to fstab to be permanent:
sudo cp /etc/fstab /etc/fstab.bak
sudo echo '/var/1GB.swap none swap sw 0 0' | sudo tee -a /etc/fstab
sudo echo '/var/1GB.swap none swap sw 0 0' | sudo tee -a /etc/fstab
Check swappiness system param. Ideally to be close to 0 if amount of RAM is sufficient. Optimal value is 10:
sudo cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
sudo vim /etc/sysctl.conf; add following line:
vm.swappiness=10