This article was published on Medium a few months ago, I repost it here:
Attempt to manage Swap less stupidly
Debian-based Linux distributions, with a small amount of RAM, seem to swap continuously, slowing down the entire system.
This script does what the system does not do by default, which is to try to empty the swap regularly to prevent the user from having to restart his PC after a few hours of use:
#! /bin/sh echo "* */1 * * * root swapoff -a && swapon -a" >> /etc/crontab
I wrote this first thinking of novice users of Ubuntu who often have a computer with modest specifications.
I’m sure this kind of script can seem very stupid (I also planned to write an article on how to repair your car with tape). But in fact, throughout my little experience, I have seen good practical results after putting it in place.
Fell free to discuss how best to work around this problem or how to improve this bash line!
I opened a thread on Stack Exchange about it: Is it a bad idea to create a cron script that empties the SWAP every hour?
EDIT: I discovered that https://askubuntu.com/a/90399/15811 was a better way to do that!
Some man pages to go further:
crontab(5): tables for driving cron A crontab file contains instructions to the cron(8) daemon of the general form: “run this command at this time on this…
swapoff(8) swapoff disables swapping on the specified devices and files. When the -a flag is given, swapping is disabled on all…
swapon(8) swapon is used to specify devices on which paging and swapping are to take place.