How to Clear Linux Memory Cache

Need to clear Linux caches for testing or troubleshooting purposes?

Run the following command to clear both the page cache and the directory entry (dentry) and inode caches:

echo 1 > /proc/sys/vm/drop_caches && echo 2 > /proc/sys/vm/drop_caches

What Does This Command Do?

The command performs two cache-clearing operations:

Clear the Page Cache

echo 1 > /proc/sys/vm/drop_caches

This clears the page cache, which stores file contents in memory to improve disk read performance.

Clear Dentry and Inode Caches

echo 2 > /proc/sys/vm/drop_caches

This clears the directory entry (dentry) and inode caches, which Linux uses to quickly locate files and directories.

A Simpler Alternative

Instead of running two separate commands, you can clear all reclaimable caches with a single command:

echo 3 > /proc/sys/vm/drop_caches

This clears the page cache, dentry cache, and inode cache in one go, making it the preferred option for most troubleshooting scenarios.

Note: Dropping caches does not free memory used by running applications. Linux automatically rebuilds these caches as files are accessed, so this command should generally only be used for testing or troubleshooting.

#linux

0 Comments