21

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.

6
  • 1
    define didn't work
    – daisy
    Commented Dec 15, 2012 at 14:30
  • 5
    What's wrong with having something cached? It could speed up access to the cached data.
    – ott--
    Commented Dec 15, 2012 at 14:43
  • 4
    What problem are you trying to solve?
    – jippie
    Commented Dec 15, 2012 at 14:59
  • 1
    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.
    – Joe
    Commented Nov 18, 2014 at 15:58
  • 4
    @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. Commented Oct 20, 2016 at 13:23

2 Answers 2

29

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.

If you want to run it with sudo, ( thanks for Evhz's comment ):

sudo sh -c "echo 1 > /proc/sys/vm/drop_caches" # or 2, 3 per your needs.
16

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

2
  • 6
    sudo sh -c "echo 1 > /proc/sys/vm/drop_caches"
    – Evhz
    Commented Dec 22, 2017 at 8:34
  • @Evhz Your comment should be a new answer and the accepted answer. The current accepted solution gives "Denied" even if using sudo echo 1 > ...
    – Basj
    Commented Jul 28, 2021 at 9:22

Not the answer you're looking for? Browse other questions tagged .