Manage SWAP space Settings in Linux

We learned in our last tutorials how to add SWAP and what is actually SWAP space used for. If you havent read this tutorial you can find it here - How To Add Swap to Linux Box. But SWAP space comes with full bag of tricks and it can impact your System depending on the configuration you apply to it. In out /etc/sysctl.conf file we have a parameter called swappiness that controls when your system should start using the SWAP space. OK what do we mean by control ?

  • when ever the total used memory reached the swappiness  parameter value the system will start using the SWAP space. This is not so good when this parameter is set to its default value of 60.
Swappiness  holds a value between 0 and 100 that represents a percentage. So when we have swappiness  set to 60 it means that when you system used 60% of its memory it will start swapping. How can we change it ?
  • to change the value of the swappiness  parameter you need to use the sysctl command:
sysctl vm.swappiness=<new value

eg:
sysctl vm.swappiness=20
  • this will only be available until the next reboot.
To make this change permanent you need to edit the  /etc/sysctl.conf file.
echo "vm.swappiness=20"  /etc/sysctl.conf
There other parameters that can be edited but they go beyond the scope of this tutorials. i hope this was helpful