Cyberithub

How to Drop/Flush/Clear Cache Memory or RAM in Linux (RedHat/CentOS 7/8) in 6 Best Steps

Advertisements

In this article, I will take you through different steps to drop/flush/clear cache memory in Linux. As you might be aware Linux has very robust Memory Management System but still if you need to clear cache memory due to certain reasons then you need to do it manually.

Someday if you ran into a problem where you find that updated data is not visible or accessible from Page Cache then you might need to clear cache memory once and check if it helps. This is required because once the cache memory is cleared then System has to access the files from Disk and hence you will get the updated data. The only downside is that clearing Cache memory will slow down your systems atleast until the cache re-build takes place.

What is Cache

Cache are usually a small reserve amount of memory used generally for faster access of disk files and directories. This Cache is generally called as Page Cache in Linux.

How Page Cache Works in Linux

Page cache is the main Linux disk Cache used in Linux. System will usually add a page based on User read process request. If the requested page is not available in the Cache then the page will be added to the disk and will be available as long as it is needed. This increases the performance of Input Output Read Operations. The only criteria is that Cache should be enough memory available. Page Cache also needs to be in sync with the disk files as new changes in the file should be synced with Cached data or it will marked as dirty and eventually will be removed from the Page Cache.

How to Check Linux Cache Memory

You might be aware of free command in Linux command line to check the current memory usage of the System. Using this command, you can check the total memory, used memory, cache or buffer memory, available memory etc as can be observed from below output. As you can see from below output, 137 MB is the current buffer/Cache memory.

[root@localhost ~]# free -m
     total used  free shared buff/cache available
Mem:  2797 144   2515    8       137      2504
Swap: 2247  0    2247

How to Drop/Flush/Clear Cache Memory or RAM in Linux (RedHat/CentOS 7/8) in 6 Best Steps 1

How to Clear Cache Memory in Linux

Also Read: 10 Useful swapon command examples in Linux (RedHat/CentOS 7/8)

Example 1: How to Clear Page Cache Only 

If you want to clear your disk cache then you need to run echo 1 > /proc/sys/vm/drop_caches after running sync command as shown below.

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

Example 2: How to Clear Page Cache Every day through crontab

You can also set a small script in crontab to clear cache memory every day as shown below. Please note that clearing cache memory everyday might slow down your system. Hence this needs to be carefully setup.

#!/bin/sh
sudo sh -c "sync; echo 1 > /proc/sys/vm/drop_caches"

Example 3: How to Clear dentries and inodes Only 

If you want to clear your dentries and inodes then you need to run echo 2 > /proc/sys/vm/drop_caches after running sync command as shown below.

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

Example 4: How to Clear dentries and inodes every day through crontab

You can also set a small script in crontab to clear page cache every day as shown below. Please note that clearing cache memory everyday might slow down your system. Hence this needs to be carefully setup.

#!/bin/sh
sudo sh -c "sync; echo 2 > /proc/sys/vm/drop_caches"

Example 5: How to Clear Page Cache, dentries and inodes 

If you want to clear your all disk cache, dentries and inodes then you need to run echo 3 > /proc/sys/vm/drop_caches after running sync command as shown below.

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

Example 6: How to Clear Page Cache, dentries and inodes every day through crontab

You can also set a small script in crontab to clear cache memory every day as shown below. Please note that clearing cache memory everyday might slow down your system. Hence this needs to be carefully setup.

#!/bin/sh
sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

 

 

 

Popular Recommendations:-

Understanding Kafka Console Producer and Consumer in 10 Easy Steps

Popular firewalld examples to open a port on RedHat/CentOS 7

How to Clear Cache Memory and Buffer Memory in Linux

How to Clear Cache Memory on Linux

Using 3 Steps – How to create an IAM User and attach policy in AWS

10 Best s3cmd Command Examples for AWS Cloud Administrators

Popular Apache Kafka Architecture Explained Using 4 Basic Components

5 Easy Steps to recover LVM2 Partition , PV , VG , LVM metadata in Linux

For Beginners: Create an EC2 Instance in AWS with 7 Easy Steps

Modprobe command in Linux

Understanding the Linux Kernel

Leave a Comment