How to Clear Cache Memory
Linux has implemented a memory management efficiently.If any process is eating away your memory and you want to clear it.Linux provides a way to flush or clear ram cache.
Every Linux System has three options to clear cache without interrupting 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
Explain above command
Sync will flush the file system buffer.Command Separated by “;” run sequentially.The Shell wait for each command to terminate before executing the next command in the sequence.drop_cache will clean cache without killing any application/service.echo is doing the job of writing to file
Now we will create a shell script to auto clear RAM cache daily at 2am via cron scheduler task.Create a shell script clearcache.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 3 > /proc/sys/vm/drop_caches"
Set execute permission on the clearcache.sh file.
# chmod 755 clearcache.sh
Now you may call the script whenever you requires to clear ram cache
Now set a cron to clear RAM cache everyday at 2am.Open crontab for editing
# crontab -e
Append the bellow line,save and exit to run it at 2am daily
0 2 * * * /path/to/clearcache.sh
For more details on How to cron a job