Clear RAM Memory Cache, Buffer and Swap
How to Clear Cache in Linux?
Every Linux System has three options to clear cache without interrupting any processes or services.
1. Clear PageCache only.
sync; echo 1 > /proc/sys/vm/drop_caches
2. Clear dentries and inodes.
sync; echo 2 > /proc/sys/vm/drop_caches
3. Clear PageCache, dentries and inodes.
sync; echo 3 > /proc/sys/vm/drop_caches
How to Clear Swap Space in Linux?
If you want to clear Swap space, you may like to run the below command.
swapoff -a && swapon -a
Also you may add above command to a cron script above, after understanding all the associated risk.
Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.