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 |