By setting the value of swappiness, you can change the behavior of the OS. The value of swappiness ranges from 0 to 100.
swappiness = 0 means avoid swap memory
swappiness = 100 means use swap memory to its fullest.
Both Ubuntu and Debian OS come with 60 as the default value of swappiness. That's why your system is using swap memory even if RAM is available.
Run the following command to check the value of swappiness on your system.
cat /proc/sys/vm/swappiness
You can change the value of swappiness by updating /etc/sysctl.conf.
Open /etc/sysctl.conf as root and change or add this line to the file:
vm.swappiness = 10
To apply the change, run the following command or reboot the system.
sysctl -p
Now, your system will use swap memory when RAM is only 10% free.