How to Clear Memory Cache in Linux

Linux Systems have three options to clear cache without interrupting any processes or services. Follow the steps how to clear memory cache in Linux.


1. Clear PageCache Only

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

  • What it does: Frees page cache (cached files).
  • Use case: When you want to clear cached file data.

2. Clear Dentries and Inodes

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

  • What it does: Frees dentries (directory entries) and inodes (file metadata).
  • Use case: When you want to clear metadata, but not the actual cached files.

3. Clear PageCache, Dentries, and Inodes

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

  • What it does: Frees page cache, dentries, and inodes.
  • Use case: When you want to clear everything cache-related.

Important Notes:

  • Always run sync before clearing cache to make sure all cached data is written to disk, preventing potential data loss.
  • You need root privileges to execute these commands. Use sudo if you are not logged in as root:bashsudo sh -c ‘sync; echo 3 > /proc/sys/vm/drop_caches’
  • This is generally safe, but clearing the cache might slow down your system temporarily as it will need to reload files into memory.

Summary Table:

Cache to Clear Command
PageCache only sync; echo 1 > /proc/sys/vm/drop_caches
Dentries and Inodes only sync; echo 2 > /proc/sys/vm/drop_caches
PageCache, Dentries, Inodes sync; echo 3 > /proc/sys/vm/drop_caches

 

Avatar photo

Asif Khan

Responsible and proactive professional with more than 14 years of experience in IT systems, open source software applications, DevOps, Linux systems, and cloud operations. My main goals are to automate things, keep them safe, and make sure they are strong. I am very good at planning and building the infrastructure for services that people really want. I was drawn to the fast-paced world of cloud computing because it has resources that can be scaled up or down as needed. One of my best skills is being able to use a lot of different DevOps tools to set up, release management, and microservices ecosystems, as well as for provisioning, orchestration, and configuration management.