Member-only story
clear the memory cache in Linux
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:
- Flush filesystem buffers to disk:
sync- Drop caches:
echo 3 > /proc/sys/vm/drop_cachesExplanation of Commands
- 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.
- echo 3 > /proc/sys/vm/drop_caches: This command clears the caches by:
1: Dropping page cache2: Dropping dentries and inodes3: 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_cachesThis ensures that all filesystem buffers are flushed before clearing the caches.







