Ticker

6/recent/ticker-posts

how to clear cache in linux

Linux Cache Clearing Commands

Linux Cache Clearing Commands Explained

Cache Types

  • PageCache: Memory used to cache disk pages for faster access.
  • Dentries and Inodes:
    • Dentries: Structures that manage the directory structure.
    • Inodes: Store metadata about files (ownership, permissions, etc.).

Commands Explained

1. Clear PageCache Only

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

Explanation:

  • sync: Ensures all pending disk writes are completed.
  • echo 1 > /proc/sys/vm/drop_caches: Clears only the PageCache.

2. Clear Dentries and Inodes

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

Explanation: Clears only the dentries and inodes.

3. Clear PageCache, Dentries, and Inodes

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

Explanation: Clears all caches: PageCache, dentries, and inodes.

4. Clear RAM Cache and Swap



sync; echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'


Explanation:

  • sync: Ensures all writes are completed.
  • echo 3 > /proc/sys/vm/drop_caches: Clears all caches.
  • swapoff -a: Disables all swap space.
  • swapon -a: Re-enables all swap space.
  • printf '\n%s\n' 'Ram-cache and Swap Cleared': Outputs a confirmation message.

Caution

  • Performance Impact: Clearing caches can temporarily degrade performance.
  • Use with Care: These commands are for troubleshooting or maintenance.

Privileges

Most commands require root privileges; use sudo to run them.

Post a Comment

0 Comments