Create a swap file to let OS use when the physical memory is not big enough. Make the swap file as 1~2x big as your physical memory.
How much swap size do you need
Based on your physical memory size, here is a chart you can use:
- 1G -> 2G
- 4G -> 6G
- 6G -> 8G
- 8G -> 11G
- 16G -> 20G
- 32G -> 38G
Create swap file
CreateSwap.sh:
#!/usr/bin/env bash
fallocate -l 12G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
Then use swapon --show
to check current swaps.
If your system already configured other swaps, you can use swapoff
to remove it, for example:
swapoff /dev/sdb
Also remember to remove it in the /etc/fstab
file such that it won't get used when system rebooted next time.
Top comments (1)
We can also use the
parted
command to create the swap partition.Here are examples to do that: