Generally set 1-2 times the RAM size according to your needs.
View the current swap partition swap
sudo swapon -s
sudo swapon --show
cat /proc/swaps
free -h
Method one: use swap file
If there is already
sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1G count=8
sudo chmod 600 /swapfile
sudo mkswap /swapfile
Enable:
sudo swapon /swapfile
will add the swap file to /etc/fstab at the next startup
/swapfile none swap sw 0 0
Method 2: Use the swap partition
sudo fdisk /dev/sda
If you already have a swap partition, you can use fdisk, or so
sudo swapoff /dev/sda3
sudo rm / dev / sda3
Now press n and press the enter key, if the first sector is ok (usually it is fine).
Set up the new partition using the following format: X is the letter of the drive and Y is the number of the partition.
sudo mkswap /dev/sdXY
比如mkswap /dev/sda3
Add partition to /etc/fstab
ls -al /dev/disk/by-uuid command to view the uuid of the swap partition
Say something like the following and add it to /etc/fstab,
UUID=236ccb97-4532-423f-a14c-2ecc3258a6236 none swap sw 0 0
df -h check which hard drive has space to add swap partition