×


Clear RAM Cache, Buffer, and Swap Space on Linux System - How to do it ?

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.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Linux System Monitoring tasks.

In this context, we shall look into Clear RAM Cache, Buffer, and Swap Space on any Linux Distribution.


1. Command to clear the Cache Page ?

# sync; echo 1 > /proc/sys/vm/drop_caches


2. Command to clear dentries and inodes

# sync; echo 2 > /proc/sys/vm/drop_caches


3. Command to clear 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.


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.


How to 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.


[Need to fix any Linux System errors? We can help you. ]


Conclusion

This article covers how to clear the cache and buffer memory of the physical memory along with clearing the swap space when needed.

Every Linux System has three options to clear cache without interrupting any processes or services.


If you want to clear Swap space, you may like to run the below command.

$ swapoff -a && swapon -a


To Clear PageCache, dentries and inodes:

$ sync; echo 3 > /proc/sys/vm/drop_caches


To Clear PageCache only:

$ sync; echo 1 > /proc/sys/vm/drop_caches


To Clear dentries and inodes:

$ sync; echo 2 > /proc/sys/vm/drop_caches