Free up cache and buffers on a Linux system
Linux OS having great features in memory management that system used to clear the cache of the buffer periodically itself but sometimes system uses so much memory for disk cache or we want some free memory from volatile storage which is not usable by RAM and even if it is not in used RAM is not in used. Read the data from cache is faster than using disk of course , It can be possible for some data system not have cache for that data, so we can flush the buffer cache or cache memory .
Here we will see kernel drop clean caches as well as reclaim inodes and dentries memory become free.
Free up the buffers cache :
Some signals in linux kernel parameter will drop various aspects of cached properly.Changing the numeric value in these parameter will free pagecache , inodes and dentries.
These below commands will be run as root user, if you are using linux through sudo user privilege you need to use sudo with some more parameter. Find below for both root user entry as well as sudo user Entry for free cache.
For linux root user
Free pagecache :
# echo 1 > /proc/sys/vm/drop_caches
Free dentries and inodes:
# echo 2 > /proc/sys/vm/drop_caches
Free pagecache, dentries and inodes:
# echo 3 > /proc/sys/vm/drop_caches
For sudo privileged users :
To clear pagecache :
$ sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'
To clear dentries and inodes:
$ sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches'
To clear pagecache, dentries and inodes:
$ sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'
How to setup automated clear memory cache through shell script and cron ?
Here we are creating a shell script to automate the free up memory cache. Now create a bash shell
Script file like we are creating file named as freecache.sh and insert below lines into this.
#!/bin/bash # shell script by www.techtransit.org , we are using echo 3 which will free pagecache,dentries and inodes. echo 3 > /proc/sys/vm/drop_caches
Now set the executable permission on the bash shell script file.
# chmod 755 freecache.sh
Let schedule this script on daily basis in cron in after midnight at 1 A.M like below.
Run the crontab –e command like below.
# crontab –e
Insert below line in crontab file.
0 1 * * * freecache.sh