Our machines have various important factors which used to work together to work a machine properly and efficiently. These factors could different from machine to machine and also usage of machine.

Some of these factors are CPU Speed, amount and speed of Memory, Disk Speed, Cache, Machine cooling mechanism. Today we will talk about one important factor which comes in picture when machine feels that it has less memory and could have memory issues in near future.

So this concept is called Virtual Memory or Swap. As we said its virtual memory, this is not actual main memory. So this is part of Hard-disk which act as RAM.In one of earlier post we mentioned how we can add swap in Linux machine

For this swap thing Linux kernel has introduce one concept which decide when should start using Swap instead of RAM or not using it at all till RAM is completely utilised.

How swappiness work

So In Linux kernel parameter called swappiness, which used to calculate at which rate swap will start consume on machine, like by default swappiess is 60 that means when RAM is 40% utilised then machine start using swap partitions(we could have multiple swap partitions, but combine while using and showing on machine).
So this was quit useful setting for those days when had expensive and less memory in machines, that time swap support various application which need more RAM. But now days machines have enough memory that rarely used completely, so we rarely need to use swap, but what if kernel still using same swappiness setting.
let’s take an example.

My Laptop have 16 GB of memory and 8 GB of swap. If I use 60 swappiness in kernel parameter then after using around 6 GB of memory my machine start using swap, which unnecessary make my application slow. SO no we should know how to change this kernel parameter.

How to check

Below is by-default swappiness value, we can change it though two ways.

#sysctl -p| grep swappiness
vm.swappiness = 60

# cat /proc/sys/vm/swappiness
60

So now we can check Machine swapiness values and ways to change it.

How to change

To change swapiness value we can make changes through sysctl command like below mentioned way

# sysctl  vm.swappiness=20
vm.swappiness = 20
# cat /proc/sys/vm/swappiness
20

Through above way we see how we can check and update value in Linux machine.But we should always make change persistent in its file-system so that we could always get these changes over reboot as well. For this we need to make changes in /etc/sysctl.conf file in below way.

▶ grep swappiness /etc/sysctl.conf 
vm.swappiness=60

After this swappiness is persistent over reboots. Now once main memory is utilised over over 80% it’s start using swap.