Sitemap
Nirbhay Singh

Nirbhay Singh

Technophile, Assistant Vice President at CITI, talks about AI, DevOps, Cloud, SRE, Linux and Programming. https://buymeacoffee.com/nirbhaysingh281

Member-only story

clear the memory cache in Linux

2 min readAug 2, 2024

--

The echo 3 > /proc/sys/vm/drop_caches command is used to clear the memory cache in Linux. This command can free up memory by dropping pagecache, dentries, and inodes.

However, to ensure data consistency and prevent data loss, it is recommended to use the sync command before dropping caches. The sync command flushes filesystem buffers, ensuring that all in-memory data is written to disk.

Here is the sequence of commands using sync:

  1. Flush filesystem buffers to disk:
sync
  1. Drop caches:
echo 3 > /proc/sys/vm/drop_caches

Explanation of Commands

  1. sync: This command forces all pending I/O operations to complete, ensuring that all data is written from memory to disk. It helps in preventing potential data loss by making sure that the file system is in a consistent state.
  2. echo 3 > /proc/sys/vm/drop_caches: This command clears the caches by:
  • 1: Dropping page cache
  • 2: Dropping dentries and inodes
  • 3: Dropping page cache, dentries, and inodes (combination of 1 and 2)

Combined Command

You can combine both commands in a single line for convenience:

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

This ensures that all filesystem buffers are flushed before clearing the caches.

--

--

Technophile, Assistant Vice President at CITI, talks about AI, DevOps, Cloud, SRE, Linux and Programming. https://buymeacoffee.com/nirbhaysingh281