Reference: https://linuxize.com/post/how-to-add-swap-space-on-ubuntu-18-04/
First look at the swap situation:
sudo swapon --show
You may need to close swap before operation:
sudo swapoff -v /swapfile
Set 1g
sudo fallocate -l 1G /swapfile
If it doesn't work, use this:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Change permissions:
sudo chmod 600 /swapfile
Create swap:
sudo mkswap /swapfile
Enable swap:
sudo swapon / swapfile
Permanently change:
sudo nano /etc/fstab
Add this line:
/swapfile swap swap defaults 0 0
Look again:
sudo swapon --show
sudo free -h
swappiess: is a Linux kernel attribute, swap usage frequency is 0-100
View:
cat /proc/sys/vm/swappiness
modify:
sudo sysctl vm.swappiness=10
Delete swap
1:sudo swapoff -v /swapfile
2: Delete line from /etc/fstab
/swapfile swap swap defaults 0 0
3:sudo rm /swapfile