How to Clear RAM Memory Cache & Buffer & Swap Space on Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

In this guide, we will talk about how to clear RAM memory cache and issues which  occur in the Linux and if you want to clear the RAM Memory cache and free some memory in Linux, there are many commands for this process.

Introduction

 Hi gyus, In computing, a cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by reading data from the cache, which is faster than recomputing a result or reading from a slower data store; thus, the more requests that can be served from the cache, the faster the system performs.

How to Clear RAM Memory Cache

Clear cache without affecting any processes or services:

  • Clear Page Cache only with the following command:
sync; echo 1 > /proc/sys/vm/drop_caches
  • Clear dentries and inodes.
sync; echo 2 > /proc/sys/vm/drop_caches
  • Clear dentries, inodes and page cache.
sync; echo 3 > /proc/sys/vm/drop_caches

Warning:

It is not recommended to use the third option “echo 3 ” in production.

NOTES:

sync will flush the file system buffer.

“;” Command separated by it to run sequentially.

drop_caches can clean the cache without troubling  any application.

Clearing Swap Space in Linux

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

swapoff -a && 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.

You also can add the above command to a cron script.

We will make a proper script with the commands above to clear RAM Cache and Swap Space.

vim clear_M_S_cache.sh

Add the following:

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

Then execute and run the script:

chmod +x clear_M_S_cache.sh
./clear_M_S_cache.sh

After that, the Ram cache and swap will be cleared so you can run the command “free -h” before and after running the script and will check the cache.

Conclusion

In this article, we illustrated how to clear and free memory cache and swap space in linux.

Thanks.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
MQ-Jr
MQ-Jr
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook