Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:linux-tutorials:swap [2023/07/30 12:12] – removed - external edit (Unknown date) 127.0.0.1linux:linux-tutorials:swap [2025/01/21 18:25] (current) – [Disable SWAP permanently] Zyzonix
Line 1: Line 1:
 +===== 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:
 +<code bash>cat /proc/sys/vm/swappiness</code>
 +
 +To change the swappiness permanently, edit ''/etc/sysctl.conf'':
 +<code bash>vm.swappiness=<value></code>
 +The variable ''vm.swappiness'' can have a value between 0 and 100, higher values mean a faster "outswapping" to the disk. 
 +
 +//Sourced from [[https://linuxize.com/post/how-to-change-the-swappiness-value-in-linux/|linuxize.com]]//
 +
 +==== Add SWAP ====
 +**Create SWAP file:**
 +<code bash>sudo fallocate -l 4G /swapfile</code>
 +→ Change 4G to the size of the swap space.
 + 
 +**Change permissions and format file:**
 +<code bash>sudo chmod 600 /swapfile</code>
 +<code bash>sudo mkswap /swapfile</code>
 +
 +**Enable SWAP and verify:**
 +<code bash>sudo swapon /swapfile</code>
 +<code bash>sudo swapon --show</code>
 +
 +**Make swap permanent by adding the following line to ''/etc/fstab'':**
 +<code bash>/swapfile none swap sw 0 0</code>
 +
 +To change the swappiness look at the upper paragraph.
 +
 +//Sourced from [[https://tecadmin.net/how-to-add-swap-in-ubuntu-24-04/|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'':
 +<code bash>/swapfile                                 none            swap    sw              0       0</code>
 +==== 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):
 +<code bash>sudo dphys-swapfile swapoff</code>
 +Then edit the SWAP size in (size in MBytes):
 +<code bash>sudo nano /etc/dphys-swapfile</code>
 +Set size to new size of swap-ram and initialize new swap file:
 +<code bash>sudo dphys-swapfile setup</code>
 +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:
 +<code bash>sudo dphys-swapfile swapon</code>
 +
 +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 [[https://pimylifeup.com/raspberry-pi-swap-file/|pimylifeup.com]]//