Linux Swap and Swapfile: Difference between revisions

From WikiMLT
Line 64: Line 64:
sudo free -h
sudo free -h
</syntaxhighlight>
</syntaxhighlight>
== Adjusting the Swappiness Value ==
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60. You can check the current swappiness value by typing the following command:
<syntaxhighlight lang="shell" line="1">
cat /proc/sys/vm/swappiness
</syntaxhighlight>
<syntaxhighlight lang="shell-session" line="1">
60
</syntaxhighlight>
While the swappiness value of 60 is OK for Desktops, for production servers you may need to set a lower value. For example, to set the swappiness value to 10, type:
<syntaxhighlight lang="shell" line="1">
sudo sysctl vm.swappiness=10
</syntaxhighlight>
To make this parameter persistent across reboots append the following line to the <code>/etc/sysctl.conf</code> file:
<syntaxhighlight lang="shell" line="1">
sudo nano /etc/sysctl.conf
</syntaxhighlight>
<syntaxhighlight lang="bash" line="1">
vm.swappiness=10
</syntaxhighlight>
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.


==References==
==References==

Revision as of 17:52, 17 August 2022

Add/​​​Remove Swap­file Short Guide

Swap On

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo nano /etc/fstab
/swapfile swap swap defaults 0 0

Swap Off

sudo swapoff -a     # sudo swapoff /swapfile
sudo rm -f /swapfile
sudo nano /etc/fstab
#/swapfile swap swap defaults 0 0

Check the swap and its us­age

sudo swapon --show
sudo free -h

Swap On/​​​Off Scripts

swap-on.sh
#!/bin/bash

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo sed 's_^#/swapfile_/swapfile_' /etc/fstab -i

echo
sudo swapon --show
echo
sudo free -h
swap-off.sh
#!/bin/bash

sudo swapoff /swapfile
sudo rm -f /swapfile
sudo sed 's_^/swapfile_#/swapfile_' /etc/fstab -i

echo
sudo swapon --show
echo
sudo free -h

Ad­just­ing the Swap­pi­ness Val­ue

Swap­pi­ness is a Lin­ux ker­nel prop­er­ty that de­fines how of­ten the sys­tem will use the swap space. Swap­pi­ness can have a val­ue be­tween 0 and 100. A low val­ue will make the ker­nel to try to avoid swap­ping when­ev­er pos­si­ble while a high­er val­ue will make the ker­nel to use the swap space more ag­gres­sive­ly.

The de­fault swap­pi­ness val­ue is 60. You can check the cur­rent swap­pi­ness val­ue by typ­ing the fol­low­ing com­mand:

cat /proc/sys/vm/swappiness
60

While the swap­pi­ness val­ue of 60 is OK for Desk­tops, for pro­duc­tion servers you may need to set a low­er val­ue. For ex­am­ple, to set the swap­pi­ness val­ue to 10, type:

sudo sysctl vm.swappiness=10

To make this pa­ra­me­ter per­sis­tent across re­boots ap­pend the fol­low­ing line to the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf
vm.swappiness=10

The op­ti­mal swap­pi­ness val­ue de­pends on your sys­tem work­load and how the mem­o­ry is be­ing used. You should ad­just this pa­ra­me­ter in small in­cre­ments to find an op­ti­mal val­ue.

Ref­er­ences