How to clear RAM Memory Cache, Buffer and Swap Space on Linux

Posted on March 24, 2022

Clear page cache only

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

Clear dentries and inodes

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

Clear page cache, dentries, and inodes

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

Now we will be creating a shell script to auto clear RAM cache daily at 3 am via a cron scheduler task.
Create a shell script clearcache.sh and add the following lines.

#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production instead use "echo 1"
echo "echo 3 > /proc/sys/vm/drop_caches"

Change the permission on the clearcache.sh using the below command

chmod 755 clearcache.sh

Next, open crontab for editing

crontab -e

Add the below line, save and exit to run it at 3 am daily.

0  3  *  *  *  /path/to/clearcache.sh

List the crontab and verify using the below command

crontab -l
to join this conversation on Samprix.Already have an account? Sign in to comment
Sponsors