How to drop caches on Linux?

This blog post explains how to drop in-memory cache of data on disk (pagecache, dentries, inodes) on Linux. Dropping the cache is useful if one wants to run filesystem benchmarks in a reproducible way.

To drop the caches, run

$ echo 3 | sudo tee /proc/sys/vm/drop_caches

The solution above is from http://www.kernel.org/doc/Documentation/sysctl/vm.txt. Some other useful commands mentioned there:

$ echo 1 | sudo tee /proc/sys/vm/drop_caches  # drop pagecache
$ echo 2 | sudo tee /proc/sys/vm/drop_caches  # drop dentries and inodes
$ echo 3 | sudo tee /proc/sys/vm/drop_caches  # drop pagecache, dentries and inodes
Note: As this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.

1 comment:

Dj Key$trike said...

Awesome! Thanks for the tips here. I use this page a lot :) Just to be clear the reason for using tee is because sudo doesn't carry thru a > file redirect. If your already root you can simply do:

echo 3 > /proc/sys/vm/drop_caches

-Ben www.portforwardpodcast.com