6

Possible Duplicate:
How to invalidate the file system cache?

I'm writing a disk intensive win32 program. The first time it runs, it runs a lot slower while it scans the user's folders using FindFirstFile()/FindNextFile().

How can I repeat this first time performance without rebooting? Is there any way to force the system to discard everything in its disk cache?

I know that if I were reading a single file, I can disable caching by passing the FILE_FLAG_NO_BUFFERING flag to a call to CreateFile(). But it doesn't seem possible to do this when searching for files.

Community
  • 1
  • 1
Steve Hanov
  • 11,316
  • 16
  • 62
  • 69
  • 1
    +1 I was getting round to posting a related post myself. When trying to work out performance of disk related algorithms, caching really gets in the way. Good question. – Tim Lloyd Jul 18 '10 at 15:55

5 Answers5

3

Have you thought about doing it on a different volume, and dismounting / remounting the volume? That will cause the vast majority of everything to be re-read from disk (though the cache down there won't care).

jrtipton
  • 2,449
  • 1
  • 15
  • 9
1

You need to create enough memory pressure to cause the memory manager and cache manager to discard the previously caches results. For the cache manager, you could try to open a large (I.e. Bigger than physical ram) file with caching enabled and then read it backwards (to avoid any sequential I/o optimizations). The interactions between vm and cache manager are a little more complex and much more dependent on os version.

There are also caches on the controller (possibly, but unlikely) and on the disk drive itself (likely). There are specific IoCtls to flush this cache, but in my experience, disk firmware is untested in this arena.

MJZ
  • 1,074
  • 6
  • 12
  • This is exactly what I’ve always done. An easy way is to open a large file and search for a sequence of bytes that don’t exist in the file. Windows will flood the cache with the new file and push the old data out. It’s not ideal, but it works. – Synetech Dec 01 '13 at 02:01
1

Check out the Clear function of CacheSet by SysInternals.

On Freund
  • 4,376
  • 2
  • 23
  • 30
  • I tried it on Windows XP and enumerating through disk folders was just as fast after clearing the cache as before, so it seemed not to work. – Steve Hanov Sep 01 '10 at 14:47
0

You could avoid a physical reboot by using a virtual machine.

Peter G.
  • 14,786
  • 7
  • 57
  • 75
0

I tried all the methods in the answers, including CacheSet, but they would not work for FindFirstFile/FindNextfile(). Here is what worked:

  • Scanning files over the network. When scanning a shared drive, it seems that windows does not cache the folders, so it is slow every time.

  • The simplest way to make any algorithm slower is to insert calls to Sleep(). This can reveal lots of problems in multi-threaded code, and that is what I was really trying to do.

Steve Hanov
  • 11,316
  • 16
  • 62
  • 69
  • 1
    To note, the Windows SMB client and Windows SMB server have their own caches that you are then dealing with (and HDD, and other caches, SAN level, RAID controller...). Your best bet, since CacheSet's magic does not work (I agree), is to "flood the cache" so that it purges previous entries. This is to say, you must figure out the size of all the involved caches and fill them up so that your previously read/written data is pushed out (or flushed to stable storage). see HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters reg_dword: TreatHostAsStableStorage. – brandeded Aug 18 '11 at 18:15