The Linux kernel provides an adjustable configuration that controls the frequency with which the swap file  is used, called swappiness

A  zero  swap setting means that the disk will be avoided unless absolutely necessary (when you run out of memory), while a swappiness setting of  100 means that the programs will be stored disk almost instantly.

The Ubuntu system comes with a standard of 60, which means that the swap file will be used quite frequently if memory usage is about half of my RAM. You can check the swappiness value of your own system by running:

one@onezero:~$ cat /proc/sys/vm/swappiness

As I have 4 GB of RAM then I would like to pass this to 10 or 15. The swap file will only be used when using my RAM is about  80  or  90  percent. To change the system swappiness value, open  /etc/sysctl.confas  root  .Then change or add this line to the file:

vm.swappiness = 10

Reboot the system for the change to take effect.

You can also change the value while the system is still running:

sysctl vm.swappiness=10

You can also clean your swap by executing  swapoff -aand then  swapon -aas root instead of restarting to achieve the same effect.