Usually, during the run time the Linux system will use an unused part of physical memory for temporary data storing. Cache and buffer refer to the temporal data of the program which the system uses to enhance the performance of the program or application. If you worried that your physical memory is being used up by the program for caching and you want to clear them. You can do so in Linux and we are going to discuss this in this article. In the Linux system, it provides three options to clear the cache, let’s look up to it.
For Clearing the Cache Page
# sync; echo 1 > /proc/sys/vm/drop_caches
For Clearing dentries and inodes
# sync; echo 2 > /proc/sys/vm/drop_caches
For Clearing PageCache, dentries and inodes
# sync; echo 3 > /proc/sys/vm/drop_caches
In the above command, the sync command flushes and writes all the cache data to disk. The semicolon (;) after the sync will separate the two commands and hold up the second command unit the first one is completely executed. Page caches are the cache data that are held by the system after reading files.
Similarly, the dentry and inode_cache are the cache data that are stored in memory after reading file attributes/directories. As per the Linux Kernel writing in the drop_caches will clear the cache without affecting the running programs.
In the above example, you can see in the cache data is decreased to 138 Mb from 249 Mb.
Note: Memory caching isn’t relatable to swap space so, increasing the swap space won’t help. It will not use swap space for storing cache data.
Clear swap memory
Basically, swap the space on the disk that is used as secondary physical memory whenever the RAM is out of memory to enhance the performance in the system. The data on the swap is also temporal so for clearing swap space in the system run the following command.
# swapoff -a && swapon -a
The swapoff command will disable the swap and swapon will enable the swap. If you want you can separately execute both commands without using operators.
In this article, we learn how to clear the cache and buffer memory of the physical memory along with clearing the swap space when needed. I hope you like this article.