1)To Clear PageCache only
echo 1 > /proc/sys/vm/drop_caches
2)To Clear dentries and inodes.
echo 2 > /proc/sys/vm/drop_caches
3) To Clear PageCache, dentries and inodes.
# sync; echo 3 > /proc/sys/vm/drop_caches
To clear memory cache and buffer cache daily keep below code in Cron Tab, So it will clear the buffer/memory cache and increase the server performance.
vi /home/Mem_Buffer_clear.sh
echo 1 > /proc/sys/vm/drop_caches
After keeping above command enter :wq to save and exit
Given required permissions to that file
chmod 755 Mem_Buffer_clear.sh
keep above shell script file in cron tab as below
crontab -e
0 3 * * * /home/Mem_Buffer_clear.sh
And to Clear Swap Space in Linux use below commands
swapoff -a && swapon -a
To clear both memory cache/buffer cache and swap Memory we can use below command and same we can keep in cron job.
echo 1 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' ' Memory Cache ,Buffer Cache and Swap Memory cleared'
Now check present Memory and usage by below commands
free -m