windows 文件与 CreateFile/ReadFile 的内存映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/641614/
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 12:08:42  来源:igfitidea点击:

Memory mapping of files vs CreateFile/ReadFile

windowswinapimemory-mapped-files

提问by

What are the drawbacks (if any) of using memory mapped file to read (regular sized files) over doing the same using CreateFile ReadFile combination?

使用内存映射文件读取(常规大小的文件)而不是使用 CreateFile ReadFile 组合进行相同操作的缺点(如果有)是什么?

回答by ReinstateMonica Larry Osterman

With ReadFile/WriteFile you have deterministic error handling semantics. When you use memory mapped files, errors are returned by throwing an exception.

使用 ReadFile/WriteFile 你有确定性的错误处理语义。当您使用内存映射文件时,通过抛出异常返回错误。

In addition, if the memory mapped file has to hit the disk (or even worse, the network) your memory read may take several seconds (or even minutes) to complete. Depending on your application, this can cause unexpected stalls.

此外,如果内存映射文件必须访问磁盘(或者更糟糕的是网络),您的内存读取可能需要几秒钟(甚至几分钟)才能完成。根据您的应用程序,这可能会导致意外停顿。

If you use ReadFile/WriteFile you can use asynchronous variants of the API to allow you to control this behavior.

如果您使用 ReadFile/WriteFile,您可以使用 API 的异步变体来允许您控制此行为。

You also have more deterministic performance if you use ReadFile, especially if your I/O pattern is predictable - memory mapped I/O is often random while as ReadFile is almost always serial (since ReadFile reads at the current file position and advances the current file position).

如果您使用 ReadFile,您还会有更多的确定性性能,特别是如果您的 I/O 模式是可预测的 - 内存映射 I/O 通常是随机的,而 ReadFile 几乎总是串行的(因为 ReadFile 在当前文件位置读取并推进当前文件位置)。

回答by facetus

A big advantage of file mapping is that it doesn't influence system cache. If your application does excessive I/O by means of ReadFile, your system cache will grow, consuming more and more physical memory. If your OS is 32 bit and you have much more than 1GB memory, than you're lucky, since on 32 bit Windows the size of system cache is limited by 1GB. Otherwise system cache will consume all available physical memory and the memory manager will soon start purging pages of other processes to disk, intensifying disk operations instead of actually lessen them. The effect is especially noticeable on 64 bit Windows, where the cache size is limited only by available physical memory. File mapping on the other hand doesn't lead to overgrowing of system cache and at the same time doesn't degrade the performance.

文件映射的一大优点是它不影响系统缓存。如果您的应用程序通过 ReadFile 进行过多的 I/O,您的系统缓存将会增长,消耗越来越多的物理内存。如果您的操作系统是 32 位,并且您的内存远远超过 1GB,那您就很幸运了,因为在 32 位 Windows 上,系统缓存的大小限制为 1GB。否则系统缓存将消耗所有可用的物理内存,内存管理器将很快开始将其他进程的页面清除到磁盘,从而加强磁盘操作而不是实际减少它们。该效果在 64 位 Windows 上尤为明显,其中缓存大小仅受可用物理内存的限制。另一方面,文件映射不会导致系统缓存过度增长,同时不会降低性能。

回答by sharptooth

You'll need more complex code for establishing the file mapping than for just opening and reading. File mapping is intended for random access to a section of file. If you don't need that, just don't bother with file mapping.

您需要更复杂的代码来建立文件映射,而不仅仅是打开和阅读。文件映射旨在随机访问文件的一部分。如果您不需要它,只需不要理会文件映射。

Also if ever need to port your code onto another platform you'll do it much easier and faster if you don't use file mapping.

此外,如果需要将您的代码移植到另一个平台上,如果您不使用文件映射,您会更容易、更快地完成。