How to Clear Memory Cache on Linux Server

We notified in most of time, we are facing low memory on Centos because so much memory for disk cache. Cache is used to keep data to use by CentOS or RHEL. Cache is used to keep information to use frequently by OS. Reading data from cache is 1000’s time speedier than reading data from hard drive. You can manually free up the memory cache with the following command:
centos

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_caches

Schedule Cron to Clear Memory Cache using Bash Script:

Presently we will be making a shell script to auto clear RAM cache every day at 12 AM by means of a cron scheduler task.

# vim /script/clearcache.sh
#!/bin/bash
echo "echo 3 > /proc/sys/vm/drop_caches"

Set execute permission on shell script file.

# chmod 755 clearcache.sh

Now set a cron to clear RAM cache everyday at 12 AM

# crontab -e
0  0  *  *  *  /script/clearcache.sh

We will run command following command before and after running the script and will check cache.

# free -m

See Also:

  • How to Check Memory Usage on CentOS/RHEL
  • Enjoy it!

    Leave a Reply

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

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    The reCAPTCHA verification period has expired. Please reload the page.