SWAP / Swappiness

Swappiness defines how fast processes, more accurate their consumed RAM, is sourced out to a disk. This is only required and recommended when there's not enough RAM available. Since hard drives are thousands of times slower than RAM, it is recommended for servers to keep the swap value as low as possible due to a higher performance. In general the physical storage location should be at least a SSD, better a NVMe.

To view the current swappiness:

cat /proc/sys/vm/swappiness

To change the swappiness permanently, edit /etc/sysctl.conf:

vm.swappiness=<value>

The variable vm.swappiness can have a value between 0 and 100, higher values mean a faster “outswapping” to the disk.

Sourced from linuxize.com

Add SWAP

Create SWAP file:

sudo fallocate -l 4G /swapfile

→ Change 4G to the size of the swap space.

Change permissions and format file:

sudo chmod 600 /swapfile
sudo mkswap /swapfile

Enable SWAP and verify:

sudo swapon /swapfile
sudo swapon --show

Make swap permanent by adding the following line to /etc/fstab:

/swapfile none swap sw 0 0

To change the swappiness look at the upper paragraph.

Sourced from tecadmin.net

Disable SWAP permanently

To disable the SWAP on Debian and some other Linux Distributions just comment out the following line in /etc/fstab:

/swapfile none swap sw 0 0

Increase SWAP-Storage (e.g. for Raspberry Pi)

Raspberry Pis are powerful, but sometimes the amount of RAM is too low, besides RAM-compression swapping can be a helpful solution.

Change/Increade SWAP Space on Raspberry Pi OS (Debian):

Firstly turn off swap (moves everything from swap to the RAM):

sudo dphys-swapfile swapoff

Then edit the SWAP size in (size in MBytes):

sudo nano /etc/dphys-swapfile

Set size to new size of swap-ram and initialize new swap file:

sudo dphys-swapfile setup

Should produce something like this: want /var/swap=4096MByte, checking existing: deleting wrong size file (2147483648), generating swapfile … of 4096MBytes Finally turn the swap back on, there's no reboot required:

sudo dphys-swapfile swapon

If getting want /var/swap=4096MByte, restricting to config limit: 2048MBytes, checking existing: keeping it as error when running setup, check CONFMAX of /sbin/dphys-swapfile. And re-run this command.

Initially sourced from pimylifeup.com