If you ever want to empty it you can use this chain of commands.
You can signal the Linux Kernel to drop various aspects of cached items by changing the numeric argument to the above command.
-
To free pagecache:
# echo 1 > /proc/sys/vm/drop_caches -
To free dentries and inodes:
# echo 2 > /proc/sys/vm/drop_caches -
To free pagecache, dentries and inodes:
# echo 3 > /proc/sys/vm/drop_cachesThe above are meant to be run as root. If you're trying to do them using
sudothen you'll need to change the syntax slightly to something like these:$ sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches' $ sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches' $ sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'NOTE: There's a more esoteric version of the above command if you're into that:
$ echo "echo 1 > /proc/sys/vm/drop_caches" | sudo shWhy the change in syntax? The
/bin/echoprogram is running as root, because ofsudo, but the shell that's redirecting echo's output to the root-only file is still running as you. Your current shell does the redirection beforesudostarts.
Now we will be creating a shell script to auto clear RAM cache daily at 3 am via a cron scheduler task. Create a shell script clearramcache.sh and add the following lines:
#!/bin/bash # Note, we are using "echo 3", but it is not recommended in production instead use "echo 1" echo "echo 1 > /proc/sys/vm/drop_caches"
Set execute permission on the clearramcache.sh file.
# chmod 755 clearcache.sh
Now you may call the script whenever you are required to clear the ram cache.
Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing.
# crontab -e
Append the below line, save and exit to run it at 3 am daily.
0 2 * * * /path/to/clearramcache.sh