How to clear disk cache in Linux

sync ; sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'



edit: better safe than sorry: flush caches before, as suggested...
edit: sudo sh -c is simplier

Comments

Why would you want to clear the disk cache?

I thought that disk cache is a way of getting to your recently used files faster.
Please reply to pascal (at) tipisoft.dk
Gautier Portet said…
You want to clear cache for testing/profiling reasons, so you can run multiple tests while always starting with an empty cache. You should never do that in normal use, since the cache will have to be re-filled.
cAde said…
I hope you know about sync command, it is a good practice to use sync before you clear disk cache
Philippe said…
Thanks, I needed this to benchmark/profile some applications that need access to the same files over and over.
Unknown said…
Thanks for the hint!

Using "sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'" probably makes more sense than involving tee.

Also, your post is formatted such that I couldn't read the line (fixed width, font goes off edge) and had to dig it out of the HTML source. :-)

But again, thanks.
Oscar Garcia said…
What about sudo blockdev --flushbufs /dev/sdXY? This way you can flush only desired file system cache.

Popular posts from this blog

Python bdist_rpm & changing the package name

Returning a array of structs with dbus-glib

Choosing a CRT-Shader for Youtube