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


Clearing RAM Memory Cache, Buffer and Swap Space on Linux

 

Memory management like Clear RAM Memory Cache, Buffer and Swap Space in Linux will be discussed in this article.

 

To Clear Cache in Linux

 

Generally all the Linux System will have three options to clear cache without interrupting any services or processes.

To Clear PageCache only

[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       923M        64M       5.1M         0B       240M
-/+ buffers/cache:    682M       304M
Swap:                 2.0G        15M       1.9G

 

[root@linuxhelp ~]# sync; echo 1 > /proc/sys/vm/drop_caches 
[root@linuxhelp ~]# free -h
                      total       used       free     shared    buffers     cached
Mem:                   987M       583M       403M       5.1M         0B        38M
-/+ buffers/cache:     544M       442M
Swap:                  2.0G        15M       1.9G

 

To Clear dentries and inodes

[root@linuxhelp ~]# free -h
                      total       used       free     shared    buffers     cached
Mem:                   987M       585M       401M       5.1M         0B        40M
-/+ buffers/cache:     545M       442M
Swap:                  2.0G        15M       1.9G

 

[root@linuxhelp ~]# sync; echo 2 > /proc/sys/vm/drop_caches 
[root@linuxhelp ~]# free -h
                       total       used       free     shared    buffers     cached
Mem:                    987M       576M       410M       5.1M         0B        42M
-/+ buffers/cache:      533M       453M
Swap:                   2.0G        15M       1.9G

To Clear PageCache, dentries and inodes

[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       584M       402M       5.1M         0B        50M
-/+ buffers/cache:    533M       453M
Swap:                 2.0G        15M       1.9G

 

[root@linuxhelp ~]# sync; echo 3 > /proc/sys/vm/drop_caches 
[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       572M       414M       5.1M         0B        38M
-/+ buffers/cache:    534M       453M
Swap:                 2.0G        15M       1.9G

sync command will flush the file system buffer. To drop_cache will clean cache without killing any application, echo command is doing the job of writing to file.

 

"...echo 1 > …." will clear the PageCache only. It is not recommended to use third option above

"...echo 3 >" can clear PageCache, dentries and inodes.

 

Using Linux Kernel, to free Buffer and Cache in Linux. Create a shell script to auto clear RAM cache daily, through a cron scheduler task. To Create a shell script script.sh by adding the below lines.

[root@linuxhelp ~]# vim script.sh
echo "echo 3 > /proc/sys/vm/drop_caches"

 

To set run permission on the script.sh file

 

[root@linuxhelp ~]# chmod 755 script.sh

To clear ram cache, you may call the script whenever required.

Setting a cron to clear RAM cache everyday 2 hours.

[root@linuxhelp ~]# crontab -e
Append the following line, save and exit to execute it at 2 hours daily.

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

To clear automatically the RAM cache on production server

On the scheduled time, the script executes and clear everything in cache. Now all users are fetching data from disk. It will result in server crash and corrupt the database.

 

To Clear Swap Space in Linux

 

If you need to clear Swap space, run the following command.

[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       602M       384M       5.1M         0B        71M
-/+ buffers/cache:    531M       455M
Swap:                 2.0G        15M       1.9G

[root@linuxhelp ~]# swapoff -a
[root@linuxhelp ~]# swapon -a
[root@linuxhelp ~]# free -h
                    total       used       free     shared    buffers     cached
Mem:                 987M       615M       371M       8.1M        12K        72M
-/+ buffers/cache:   543M       443M
Swap:                2.0G         0B       2.0G

Add the above command to a cron script, after understanding all the risk. Here we will combine both the commands into one single command, to form a proper script to clear Swap Space and RAM Cache.

[root@linuxhelp ~]# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '
%s
' 'Ram-cache and the swap get cleared'

Now the Ram-cache and the swap gets cleared.

$ su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '
%s
' 'Ram-cache and the swap get cleared’ root

After testing above commands, we will execute "free -h" command before and after executing the script and then we will check the cache.

[root@linuxhelp ~]# free -h
                       total       used       free     shared    buffers     cached
Mem:                    987M       588M       399M       8.1M       1.4M        43M
-/+ buffers/cache:      543M       444M
Swap:                   2.0G         0B       2.0G

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Cache
user image Author :  Mason