Thursday, February 14, 2008

Drop Cache

Ok, this isn't a Vim tip, but it does apply to development that a lot of people do with Vim. I do a lot of SQL work here at Grooveshark and constantly wrestle with trying to get an accurate picture of how efficient a given query is. Linux and MySQL both cache very aggressively, which makes benchmarking a pain. MySQL provides the SQL_NO_CACHE query option to avoid it's query cache, which is convenient; however, Linux still caches the disk reads; thereby, skewing any performance measurements. In the past, I've been forced to do ridiculous things like cat a file larger than available RAM to /dev/null or unmount and remount the filesystem that the database files are housed on. Luckily, Linux kernels 2.6.16 and newer provide a mechanism to clear the inode, page, and dentry caches on demand avoiding all this headache. All you have to do is echo a value to the proc filesystem, and you're done.

To free pagecache, dentries and inodes:

echo 3 > /proc/sys/vm/drop_caches

Note: this must be done as root, and you should issue a sync before doing so.

5 comments:

Anonymous said...

This is great. I've been looking for a solution to this problem for days. The fact that nobody knows the answer to such as seemingly basic question is ridiculous and means that nobody out there is even trying to optimize their queries.

Could you explain a bit about syncing, please? Thanks.

Travis Whitton said...

Sure, Linux normally writes data buffered in memory out to disk at a specified interval. This provides a nice performance benefit, but there are certain cases where you want to guarantee that everything is flushed to disk. If you type sync when logged in as root, it tells the OS to write all buffered superblock, inode, buffered read, and buffered write data to disk. This insures that nothing is sitting in RAM before you flush the cache.

You can also issue a sync after an operation such as copying a large file to insure that all the data is committed to disk. Conveniently, sync will block until the data is written giving you a guarantee that you're safe.

Anonymous said...

I'm guessing that the idea with the sync is that dropping the cache will only drop "clean caches", which doesn't include updated blocks which haven't been written to disk yet. After you do a sync, then there will be no unwritten blocks in memory any more, and so all of the cache will then be clearable by a drop.

Anonymous said...
This comment has been removed by a blog administrator.
loews philadelphia hotel said...

Such a usefule blog! The problem is I don't think I would want to leave. Thanks for sharing such kind of post.