How to clear memory cache in Linux

163,701

Solution 1

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

Solution 2

Depending on what you want to do you can use 1,2 or 3

from https://www.kernel.org/doc/Documentation/sysctl/vm.txt

drop_caches

Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.

To free pagecache:

echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

echo 3 > /proc/sys/vm/drop_caches

As this is a non-destructive operation and dirty objects are not freeable, the user should run 'sync' first.

Share:
163,701

Related videos on Youtube

OmiPenguin
Author by

OmiPenguin

Proud to be part of Linux Family.

Updated on September 18, 2022

Comments

  • OmiPenguin
    OmiPenguin over 1 year

    TOP

    Is there any command that by using I can clean the cache in RHEL?

    I used this command:

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

    but it didn't work.

    • daisy
      daisy over 11 years
      define didn't work
    • OmiPenguin
      OmiPenguin over 11 years
      Means command executed successfully but results didnt changed. Means the Cache Memory remained occupied.
    • jippie
      jippie over 11 years
      What problem are you trying to solve?
    • Joe
      Joe over 9 years
      There are many reasons why the cache wouldn’t be cleared. If the file is in use or if the cache size is due to tmpfs or ramfs. I have blogged about different possibilities of cache here.
    • 0xC0000022L
      0xC0000022L over 7 years
      @ott--: simple, if you want to run high level performance tests (run version X of A against version X+1 of A to measure differences), how do you eliminate the file system cache from your testing? Because if you don't you'll skew your tests. You could by rebooting, but flushing the cache is certainly another option.
  • Evhz
    Evhz over 6 years
    sudo sh -c "echo 1 > /proc/sys/vm/drop_caches"
  • jiffy.of.eternity
    jiffy.of.eternity over 2 years
    @Evhz Your comment should be a new answer and the accepted answer. The current accepted solution gives "Denied" even if using sudo echo 1 > ...