简体   繁体   English

从内存中驱逐只读内存映射页面?

[英]Evicting read-only memory mapped pages from memory?

I have some huge binary files which I'm currently reading using memory mapping on Windows. 我有一些巨大的二进制文件,目前正在使用Windows上的内存映射读取这些文件。 Everything works fine, but problems start when the file size is larger than memory. 一切正常,但是当文件大小大于内存时,问题就开始了。 For instance, on a 24 GiB memory machine, I map a 64 GiB file and then I'm doing more or less streaming reads through it (not totally streaming, but ok'ish.) Nevertheless, the process winds up using all memory at some point. 例如,在24 GiB内存机器上,我映射了64 GiB文件,然后通过它进行或多或少的流式读取(不是完全流式传输,但还可以)。尽管如此,该过程最终占用了所有内存一点。

I'm touching each byte in the file exactly once, so it should be enough to keep let's say 4-5 GiB around for optimal performance. 我只触碰了文件中的每个字节一次,因此应该保持4-5 GiB左右以获得最佳性能。 Is there a way to force Windows (7) to discard pages? 有没有一种方法可以强制Windows(7)放弃页面? FlushViewOfFile discards dirty pages only, but in my use case, I'm only reading from the file. FlushViewOfFile仅丢弃页,但在我的用例中,我仅从文件中读取。 I've already set the NO_BUFFER flag, but I'd like to be able to reduce the page priority somehow so they get evicted more quickly. 我已经设置了NO_BUFFER标志,但是我希望能够以某种方式降低页面优先级,以便更快地将它们逐出。 Right now, once the memory is full, the app comes to a grinding halt as Windows pages out everything. 现在,一旦内存已满,随着Windows对所有内容进行分页,该应用程序将停止运行。

[Edit] It's a 64-bit application on a 64-bit windows. [编辑]它是64位Windows上的64位应用程序。

Don't map it all as a single view. 不要将所有内容都映射为一个视图。 Call CreateFileMapping once, then call MapViewOfFile and UnmapViewOfFile as you need segments of the file. 调用一次CreateFileMapping ,然后在需要文件段时调用MapViewOfFileUnmapViewOfFile UnmapViewOfFile should cause the pages to be discarded. UnmapViewOfFile应该导致页面被丢弃。

You can of course have several views from the same file mapping at once, they can be overlapping or non-overlapping, and overlapping views will be coherent (even across different file mappings and different processes). 当然,您可以一次从同一个文件映射中获得多个视图,它们可以重叠或不重叠,并且重叠的视图将是连贯的(即使在不同的文件映射和不同的过程中)。

Another option is to VirtualUnlock the pages you know you won't be using for a while. 另一个选择是VirtualUnlock您知道一段时间不会使用的页面。 If the pages are not currently locked, VirtualUnlock will remove them from the working set and place them on the standby list, just as if you unmapped them. 如果当前未锁定页面,则VirtualUnlock会将其从工作集中删除,并将它们放置在待机列表中,就像您取消映射它们一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM