Reducing SWAP Usage
Have you ever noticed that your computer may start using swap even when you aren't running out of memory such as in the screenshot below:
You can prevent this behaviour by changing an attribute called swappiness. Swappiness can take a value between 0 and 100 which represents a percentage likelyhood that your computer will use swap. For example, setting the value to 0, will force your computer to never use swap unless it absolutely has to. This can be done by executing:
sudo sysctl vm.swappiness=0
Doing it this way will take immediate effect, but for the change to be permanent, you would need to edit the
/etc/sysctl.conf file and add the line:
vm.swappiness = 0
If you have already added this line, then just update it, but it doesn't exist by default.
/etc/sysctl.conf file require a reboot to take effect.
Don't Set Swappiness to Zero
The default swappiness value is 60, and for good reason. RAM which is NOT occupied by running programs is used as disk cache. This means actual performance benefits are not straightforward, especially if you are using any traditional HDDs which are very slow to access. At the end of the day, the developers of Ubuntu had to pick a default value that worked well for people running a single hdd with only 4GB of RAM, and power users who are only using SSDs with 32 GB of RAM. For this they chose 60. With the hardware I'm usually running, I'll probably go with a value between 10 and 20.
If you just want to clear swap, but don't want to change the swappiness, then you can just execute these two commands:
sudo swapoff -a sudo swapon -a
First published: 16th August 2018