Type the following command to create 512MB swap file (1024 * 512MB = 524288 block size):
dd if=/dev/zero of=/swapfile bs=1024 count=524288
chown root:root /swapfile
chmod 0600 /swapfile
mkswap /swapfile
swapon /swapfile
Open /etc/fstab
and append the following line:
/swapfile none swap sw 0 0
To disable it:
swapoff /swapfile && swapon -s
The value in /proc/sys/vm/swappiness
file controls how aggressively the kernel will swap memory pages. Higher values increase agressiveness, lower values descrease aggressiveness. The default value is 60. To make changes permanent:
sysctl -w vm.swappiness=VALUE && sysctl -p
Fast Bash script to create a 5GB swap file:
$ cat swap-file.sh #!/bin/bash dd if=/dev/zero of=/swapfile bs=1024 count=5242880 chown root:root /swapfile chmod 0600 /swapfile mkswap /swapfile swapon /swapfile
Ensure file execution with
sudo chmod +x swap-file.sh
.