LinuxOperating Systems

How To Clear Cache / Buffer memory & set cron job on Linux

The cache / buffer memory in Linux is nothing but a set of cache stored on the server which contains frequently visited pages. The cache configuration is done in a such a way that whenever any data is requested by RAM it will check the cache memory and serve the data from cache memory if available. This helps to increase the response time and optimum usage of the server resources.

cache memory

The cache / buffer memory from RAM on Linux and Unix servers can be flushed in different ways.

How to clear cache / buffer memory on a Linux server?

Option 1:

To clear pagecache, dentries & inodes i.e. flush all from cache memory

sync; echo 3 > /proc/sys/vm/drop_caches

Option 2:

To clear only inodes and dentries from cache memory

sync; echo 2 > /proc/sys/vm/drop_caches

Option 3:

To clear only page cache from cache memory

sync; echo 1 > /proc/sys/vm/drop_caches

How to set a cron job to clear cache memory in Linux?

The cache memory may be required to cleaned up at regular intervals and free up the disk space. The same can be achieved by creating a cronjob to automatically clear the cache memory using crontab.

Step 1:

  • Access the desired server through shell as a root or sudo user.

Step 2:

  • Create a shell script say "clearcache.sh" under root  (/root) partition with following commands
#!/bin/sh
sync; echo 3 > /proc/sys/vm/drop_caches

Step 3:

  • Set the permission of "clearcache.sh" script to 755
chmod 755 /root/clearcache.sh

Step 4:

  • Edit the system crontab file using below command
crontab -e

Step 5:

  • Enter the following line in crontab file to set cron job for clearing cache memory automatically
0 * * * * /root/clearcache.sh

The above cron job will execute the "clearcache.sh" script every hour of first second.

Step 6:

  • Restart cron service
/etc/init.d/crond restart

Note: One should be a root user or sudo user to perform the above mentioned operations.

Should I clear / flush cache memory from RAM at regular intervals?

Well it depends upon the situation. On production servers it is not advisable to clear cache at regular since this will force server to serve pages from disk instead of cache till cache is not getting build up thus increasing server load and resources.

To learn more about cache, visit the LinuxAteMyRAM website. Hopefully, after referring to the website the concept about cache in Linux should get clear.

Please let us your feedback, queries by posting in comment section.

Related Articles

11 Comments

  1. Great article! everything is 100% working. Thanx. i have small vps and i have bot for firefox. after a few hours, the bot crashed due to memory overflow. now everything has been running steadily for several days. thanks a lot! @hourly option in crontab -e without restarting!

  2. Hi
    create a shell script clearcache.sh and add the following line:-
    #!/bin/bash
    echo "echo 3 > /proc/sys/vm/drop_caches"

    then
    chmod755 for file -> Open crontab for editing->0 * * * * /home/clearcache.sh

    but the script not working.
    please help me !

    1. Thanks for the comment.
      I think you have specified echo instead of sync at the start of the command. Please check step 2 under how to set a cron job.. section for correct contents.
      If your script is still not working then please post the error log in the comment.

  3. it is working, thank you. dont you think that output of the command should be redirected to null in the shell script?

  4. 1 0 * * * * /root/clearcache.sh
    the following line on cronjob for clearing cache it isn't 'every hour'
    it is every day at 00Pm : 1 minute
    example
    “At 00:01.”
    at 2018-03-24 00:01:00
    then at 2018-03-25 00:01:00
    then at 2018-03-26 00:01:00
    then at 2018-03-27 00:01:00
    then at 2018-03-28 00:01:00

    🙂

    1. Thanks for the feedback.
      The cron job is correct. The '1' before the cron job is the command number and not associated with cron job.
      However, I have updated the post now to avoid the confusion.

    1. You will need a rooted phone to be a super user in the command line OR you can use clear cache applications like Clean Master present on Google Play Store.
      This should serve your purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button