How to Clear Cache Memory in Linux: Clear RAM Memory Cache, Buffer, and Swap Space

Have you ever encountered a process that’s consuming too much memory and wanted to clear it? Linux provides a way to flush or clear the RAM cache, which is essential for optimizing your system’s performance. In this guide, we’ll show you how to clear cache memory in Linux.

Methods to Clear Cache Memory in Linux

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

1. Clear PageCache only

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

2. Clear dentries and inodes

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

3. Clear PageCache, dentries, and inodes

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

Understanding the Cache Clearing Commands

The sync command flushes the file system buffer. Commands separated by “;” run sequentially. The shell waits for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean the cache without killing any application or service. The echo command is responsible for writing to the file.

How to clear cache memory in Linux

How to Clear Swap Space in Linux?

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

swapoff -a && swapon -a

Share this to your friends if you found this article is useful.