How long does it take for Linux to drop caches?

1

I run the following to clear cached files from the filesystem/system memory:

# echo 3 > /proc/sys/vm/drop_caches

I'd like to do this on a scripted basis, so that when I do I/O testing to compare the speed of (say) two different binaries, I ensure that the particular state of the file system (and whether files being accessed are cached) minimizes any variance in speed test results.

My question is: How long does it take for the above command to do its magic? I'd like to know if I need to add some kind of programmatic pause after running the above, to help better establish the baseline I'm after.

Is the clearing effect immediate, and if not, is any delay measurable?

Alex Reynolds

Posted 2014-06-04T18:22:03.593

Reputation: 601

You can see the effect in real time, by running htop and watching its memory usage meter. (Optionally, also add another copy of the same meter in "text" mode, so that it would show both a graphical meter and the actual numbers.) After the command, "cached" memory should drop to zero immediately. – user1686 – 2014-06-04T19:07:36.903

Answers

2

Only things that can be immediately dropped are dropped by this operation. So while it is instant, it is insufficient for your purpose. For example, anything the previous operation modified that weren't yet flushed to disk are still in RAM for the next operation.

You probably want to run sync and wait a few moments for disk activity to stop, before you drop.

David Schwartz

Posted 2014-06-04T18:22:03.593

Reputation: 58 310