How to fix cache on Ubuntu

Clearing a cache on Linux Ubuntu

There are all total of three stages of clearing a cache on Linux Ubuntu.

Open your terminal with Ctrl + Alt + t and follow the given below steps:

  1. Clear Page Cache:
    bash; echo 1 > /proc/sys/vm/drop_caches
  2. Clear inodes and dentries:
    bash; echo 2 > /proc/sys/vm/drop_caches
  3. And clear inodes, page cache and dentries:
    bash; echo 3 > /proc/sys/vm/drop_caches

Talking about the above steps. Ubuntu supports bash rather than sync. And command separated by ; runs in a flow.  As we know that terminal waits for another commands sequentially which will help to reduce the work flow. We are using echo for writing in the file and also drop_caches will help to kill the cache without closing any software or application.

After that you have to create a file on terminal which is clearcache.sh which will help to auto clear RAM on a certain basis. And you have to add below command on this file:

#!/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"

 

And now set execution permission on clearcache.sh file

# chmod 755 clearcache.sh

Now, you have to clear RAM cache. Set cron to clear your RAM on daily basis by opening crontab.

# crontab -e

And after that a file will be opened. You just have to go to the last line of the code and have to append a new code.

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

Now it’s time for clearing a swap space in Linux.

# swapoff -a && swapon -a

Just run the above code to clear a swap space on Ubuntu or any Linux

After that, just copy the below code and paste it on your terminal:

# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'

After successfully running it, run command :

# free -h

It will show you your space before and after running the command.

 

You may also like...

Leave a Reply

avatar
  Subscribe  
Notify of