How to flush the disk read cache under Windows? [duplicate] How to flush the disk read cache under Windows? [duplicate] windows windows

How to flush the disk read cache under Windows? [duplicate]


Someone from Microsoft helped me out with this a few years ago (back in the days of Windows XP) and gave me a solution that met my needs at the time. I already had two drives in my machine, and what I needed to test was already on the D drive, and fortunately I didn't need to keep any files open on my D drive when I wanted to flush the cache. From the Windows Disk Management interface, I could change my D: drive letter to another letter, and then back again, and the D drive would perform as if had a cold cache in the OS.


AFAIK, it's unfortunately not possible to discard the read cache under Windows. I spent some time looking into this some years ago, and only found out how to flush the write cache.

As I see it, you have three options, unless somebody else has found some magic:

  1. If possible, do your read file I/O in unbuffered mode.
  2. Each time you want to benchmark, create a new copy of the test data specifying unbuffered mode when creating the new copy (this should keep the copy out of read cache, but I haven't tested).
  3. Allocate enough memory that windows has to discard the disk cache (ugh!).

EDIT: it is indeed possible to flush the read cache, at least on Vista and later: Disable or flush page cache on Windows. It requires a call to the undocumented NtSetSystemInformation NT API, though. Also, for a single file, read cache can be flushed simply by opening the file with FILE_FLAG_NO_BUFFERING specified and closing the handle again.


The only solution I found so far was http://chadaustin.me/2009/04/flushing-disk-cache/ but this ones takes too much time so I hope we'll find a better one.