How to clear OS cache

Modern computers are equipped with lots of memory, and operating systems utilizes the free memory space to cache things for faster access, such as inodes and files on disk.  This is great for day to day uses because the caching make things faster, unless you are an “experimental computer scientist” who often carries out serious performance tests and the OS cache would just get in the way messing up your timing information, and many of you bite the bullet by rebooting the machine each time, that is, if you have the privilege to do so, and your performance test would take much longer to finish.

Under Linux, you don’t need to reboot the machine.  You can use the following command chain to clear the OS cache (but you still need sudo access to the following command chain):

> sudo su
> sync; echo 3 > /proc/sys/vm/drop_caches

sync is to make sure all dirty buffers are flushed. writing 3 to /proc/sys/vm/drop_caches is to clear everything: pagecaches, directory entries (or dentries), and inodes. You may also choose to clear only pagecaches using “echo 1”, or clear dentries and inodes using “echo 2”.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s