Linux OS has great features in memory management that the system uses to clear the cache of the buffer periodically itself but sometimes system uses so much memory for disk cache or we want some free memory from volatile storage that is not usable by RAM and even if it is not in used RAM is not in use. Reading the data from the cache is faster than using a disk, of course, It can be possible for some data systems not to have a cache for that data, so we can flush the buffer cache or cache memory.
Here we will see kernel drop clean caches as well as reclaim inodes and dentries memory become free.
Understanding and Buffers:
Before entering into the steps, it’s important to understand the role of cache and buffers in a Linux system. Cache is a process used to store frequently accessed data temporarily, while buffers hold data temporarily during the input/output process. While these features contribute to faster access times, excessive accumulation may lead to suboptimal system performance.
Some signals in Linux kernel parameters will drop various aspects of cached properly. Changing the numeric value in this parameter will free the page cache, inodes, and entries. These below commands will be run as root user, if you are using Linux through sudo user privilege you need to use sudo with some more parameters. Find below for both root user entry as well as sudo user Entry for free.
Method 1: Clearing PageCache:
PageCache is a part of the Linux file system cachethat stores file data in memory. To clear the PageCache, you can use the following command:
sync; echo 1 > /proc/sys/vm/drop_caches
Method 2: Clearing Inode and Dentry Caches:
The below command releases the Inode and dentry caches, contributing to the overall freeing up of system memory.
sync; echo 2 > /proc/sys/vm/drop_caches
Method 3: Free page cache, dentry, and inodes
This will do all method 1 & method 2 in one step.
sync; echo 3 > /proc/sys/vm/drop_caches
Method 4: Using the sync Command:
sync command synchronizes the file system by flushing data to disk. While it doesn’t directly clear cache or buffers, it ensures that any pending changes are committed to disk before initiating commands.
Executing this command before cache clearing operations is good practice to prevent data loss.
How to set up an automated clear memory cache through shell script and cron?
Here I am creating a shell script to automate the free-up memory. Now create a bash shell
Script file like we are creating a file named freecache. sh and insert the below lines into this.
#!/bin/bash # shell script by www.techtransit.org , we are using echo 3 which will free pagecache,dentries and inodes. echo 3 > /proc/sys/vm/drop_caches
Now set the executable permission on the bash shell script file.
# chmod 755 freecache.sh
Let’s schedule this script daily in cron after midnight at 1 A.M like below.
Run the crontab –e command like below.
# crontab -e
Insert the below line in the crontab file.
0 1 * * * freecache.sh
Read More Blog Post on techtransit click Here