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

How to Clear Cache and Cookies on Dell Laptop

Clearing of RAM Memory Cache on Linux

In most of the systems, we see the caches that belong to unwanted files and they can be a threat to our system. The Cache issues  occur in the Linux cache also and if you want to clear the RAM Memory cache and free some memory in Linux, there are many commands for this process

Clearing  Ram Memory Cache in Linux:

In every Linux system, there are three methods to clear cache without being a trouble to any services or processes.

Example 1: For the PageCache clearing

Syntax :sudo sh -c ‘echo 1 >/proc/sys/vm/drop_caches’

The command # free -h will represent the status of the memory

By using the drop_caches, we can clean the cache without troubling  any application, you can run the # free -h command and see the difference between used and free memory before and after clearing the cache

Example 2: To Clear dentries and inodes

Syntax: sudo sh -c ‘echo 2 >  /proc/sys/vm/drop_caches’

Example 3: To Clear page cache, entries, and inodes

Syntax: sudo sh -c ‘echo 3 >  /proc/sys/vm/drop_caches’ 

Now using Linux Kernel, to free Buffer and Cache in Linux we will Create a shell script. This will auto-clear the  RAM cache regularly, through a cron scheduler task., by using the command vim script. sh the shell script “script. sh” is created

Now in the script, you have to add the below-given syntax:

echo ” echo 3 >  /proc/sys/vm/drop_caches”

Now to enable the run permission, to clear the ram cache, you can use the script whenever needed, setting a cron to clear RAM caches every day for 3 hours.

# chmod 755 script.sh

# crontab -e

Example 4:  Clearing Swap Space in Linux

Run the below-given command to clear the swap space step by step.

Syntax :

sudo swapoff -a

sudo swapon -a

By running the # free -h command you can  see the difference between used and free memory before and after clearing the swap space

When the above command adding to a cron script, We are going to connect these two different commands into one single command, to make a proper script that supports clear Swap Space and RAM Cache.

echo 3 >  /proc/sys/vm/drop_caches & &  swapoff -a & &  swapon -a & &  printf ‘\n%s\n’  ‘  ‘ Ram-cache and the swap get cleared’

After that, the Ram cache and swap will be cleared so you can run the # free -h command to see

At the end of running the command, you can see the below-given output.

We hope this article is very important for you in Clearing of RAM Memory Cache, Buffer, and Swap Space on Linux

Leave a Reply