Adding SWAP to a Debian Machine
So for SWAP, we usually have two choices. A swapfile, and a swap partition.
A swapfile is a lot more flexible since it lives on your filesystem. However, it does have an added overhead of the filesystem.
Another reason why you may want to use a swap partition is that in case you have more than one linux distro installed, they can share the same swap partition.
People also encrypts their swap partition
-
First we create the swapfile with 4G
fallocate -l 4G /swapfile -
Then, we set the correct permission for the swapfile
chmod 600 /swapfile -
We now run this to setup the swapfile with the correct headers.
mkswap /swapfile -
We will now run this to enable the swap partition, please note that this is a transient operation and will not persist if you reboot the system.
swapon /swapfile -
Lastly, we will add the swapfile to our fstab so it will automatically enable the swapfile on boot.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
There are also two sysctl options that you can tweak, either via the sysctl
command (transient), or /etc/sysctl.conf (persistent).
vm.swappiness
vm.vfs_cache_pressure
Further reading: kernel Memory Management docs
Adapted from the DigitalOcean SWAP space guide








