windows Windows下如何刷新磁盘读取缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3574168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:06:44  来源:igfitidea点击:

How to flush the disk read cache under Windows?

windowscaching

提问by sorin

Possible Duplicate:
How to invalidate the file system cache?

可能的重复:
如何使文件系统缓存无效?

In order to be able to so some benchmarks I need to cleanup Windows disk readcache. How can I do this?

为了能够进行一些基准测试,我需要清理 Windows 磁盘读取缓存。我怎样才能做到这一点?

In fact I want to compare if loading a big Unicode file (UTF-8 or UTF-16) from disk is faster or not, considering that in memory I do keep UTF-16.

事实上,我想比较从磁盘加载一个大的 Unicode 文件(UTF-8 或 UTF-16)是否更快,考虑到在内存中我确实保留了 UTF-16。

I know that it should be no significant difference but in order to benchmark it I need to be sure that that file is not cached - I need to see if size on disk has more or less impact than decoding the file.

我知道它应该没有显着差异,但为了对其进行基准测试,我需要确保该文件没有被缓存 - 我需要查看磁盘上的大小是否比解码文件具有更多或更少的影响。

采纳答案by sorin

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.

到目前为止,我找到的唯一解决方案是http://chadaustin.me/2009/04/flushing-disk-cache/,但这个解决方案需要太多时间,所以我希望我们能找到更好的解决方案。

回答by snemarch

AFAIK, it's unfortunately not possible to discardthe readcache under Windows.I spent some time looking into this some years ago, and only found out how to flushthe writecache.

据我所知,这是不幸的是不可能放弃读取Windows下的高速缓存。我花了一些时间寻找到这个几年前,只发现了如何刷新写入缓存。

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 shouldkeep the copy out of read cache, but I haven't tested).
  3. Allocate enough memory that windows has to discard the disk cache (ugh!).
  1. 如果可能,请在无缓冲模式下执行读取文件 I/O。
  2. 每次要进行基准测试时,请在创建新副本时创建指定无缓冲模式的测试数据的新副本(这应该使副本不在读取缓存中,但我还没有测试过)。
  3. 分配足够的内存,Windows 必须丢弃磁盘缓存(呃!)。

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 NtSetSystemInformationNT API, though. Also, for a single file, read cache can be flushed simply by opening the file with FILE_FLAG_NO_BUFFERINGspecified and closing the handle again.

编辑:确实可以刷新读取缓存,至少在 Vista 及更高版本上:禁用或刷新 Windows 上的页面缓存。但是,它需要调用未记录的NtSetSystemInformationNT API。此外,对于单个文件,可以通过打开FILE_FLAG_NO_BUFFERING指定的文件并再次关闭句柄来刷新读取缓存。

回答by Jeff Grills

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.

几年前(回到 Windows XP 时代),Microsoft 的某个人帮助我解决了这个问题,并给了我一个满足我当时需求的解决方案。我的机器里已经有两个驱动器了,我需要测试的东西已经在D盘上了,幸运的是,当我想刷新缓存时,我不需要在我的D盘上保持任何文件打开。从 Windows 磁盘管理界面,我可以将我的 D: 盘符更改为另一个盘符,然后再返回,D 盘会像操作系统中的冷缓存一样运行。