Clearing PageCache, dentries and inodes in Linux

Like any other operating system, GNU/Linux has implemented a memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache. Use the commands below to clear PageCache, dentries and inodes, or PageCache, dentries and inodes. Keep it mind it isn't a good idea to auto clear RAM cache on production servers. However, if you are troubleshooting an issue with a runaway process or need to ensure all fetching of data comes from disk clearing cache can be very helpful.

To clear PageCache only.

[root@localhost ~]# sync; echo 1 /proc/sys/vm/drop_caches

To clear dentries and inodes.

[root@localhost ~]# sync; echo 2 /proc/sys/vm/drop_caches

Clear PageCache, dentries and inodes.

[root@localhost ~]# sync; echo 3 /proc/sys/vm/drop_caches

Steps to clear cache on a recurring schedule

Step 1: Create a script to clear PageCache, dentries and inodes.

[root@localhost ~]# vi /home/linuxuser/drop_caches.sh

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

Step: Edit crontab to run the script every 59 minutes.

[root@localhost ~]# vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
  */59  *  *  *  * linuxuser /home/linuxuser/drop_caches.sh

Verify the configuration

Using the tail command we can view the last X lines of the cron log. With the command below I am viewing just the last 14 lines and I can see when the drop_caches.sh is run and when completed. In the example below the script was run at 09:59:01 and again at 10:59:01. This will be repeated every 59 minutes per the crontab configuration.

[root@localhost ~]# tail -14 /var/log/cron

Sep 12 09:59:01 localhost CROND[17716]: (linuxuser) CMD (/home/linuxuser/drop_caches.sh)
Sep 12 10:00:01 localhost CROND[17740]: (linuxuser) CMD (/home/linuxuser/drop_caches.sh)
Sep 12 10:01:01 localhost CROND[17768]: (root) CMD (run-parts /etc/cron.hourly)
Sep 12 10:01:01 localhost run-parts(/etc/cron.hourly)[17768]: starting 0anacron
Sep 12 10:01:01 localhost run-parts(/etc/cron.hourly)[17777]: finished 0anacron
Sep 12 10:01:01 localhost run-parts(/etc/cron.hourly)[17768]: starting 0yum-hourly.cron
Sep 12 10:11:29 localhost run-parts(/etc/cron.hourly)[17923]: finished 0yum-hourly.cron
Sep 12 10:59:01 localhost CROND[18610]: (linuxuser) CMD (/home/linuxuser/drop_caches.sh)
Sep 12 11:00:01 localhost CROND[18630]: (linuxuser) CMD (/home/linuxuser/drop_caches.sh)
Sep 12 11:01:01 localhost CROND[18670]: (root) CMD (run-parts /etc/cron.hourly)
Sep 12 11:01:01 localhost run-parts(/etc/cron.hourly)[18670]: starting 0anacron
Sep 12 11:01:01 localhost run-parts(/etc/cron.hourly)[18679]: finished 0anacron
Sep 12 11:01:01 localhost run-parts(/etc/cron.hourly)[18670]: starting 0yum-hourly.cron
Sep 12 11:11:56 localhost run-parts(/etc/cron.hourly)[18952]: finished 0yum-hourly.cron
Categories: CentOSLinux

Related Posts

Application Containers

Docker container management using Rancher

A container management platform is a solution used to o create cloud-native, distributed applications and package legacy applications that were not originally designed for virtual environments. Container management software simplifies the process of adding or Read more…

CentOS

Install MySQL Galera Cluster on Centos 7

MySQL Galera Cluster is a synchronous multi-master cluster, available on Linux only, and only supports the XtraDB/InnoDB storage engines . It is designed to provide high availability and high throughput with low latency, while allowing Read more…

Application Containers

Installing Docker on Centos 7

Docker is a software technology providing containers. Docker provides an additional layer of abstraction and automation of operating-system-level virtualization on Windows and Linux. Docker uses the resource isolation features of the Linux kernel such as Read more…