How can I decommit a file-mapped page?

冷暖自知 提交于 2019-12-09 20:24:36

问题


I have a memory mapped file, and a page in a view which is currently committed. I would like to decommit it. MapViewOfFile tells me I cannot use VirtualFree on file mapped pages. Is there some other way to do it?


回答1:


You cannot decommit it, but what you really want is not decommitting it ...

What you really want is to release the page from a memory. This can be done by using VirtualUnlock. See VirtualUnlock Remarks:

Calling VirtualUnlock on a range of memory that is not locked releases the pages from the process's working set.

Note: As documented, the function will return FALSE (the page was not locked) and GetLastError will return ERROR_NOT_LOCKED.

This is described in Guillermo Prandi's question CreateFileMapping, MapViewOfFile, how to avoid holding up the system memory.

Remarks: I think you can view it this way: decommitting a mapped page is nonsense - pages is commited whenever it is backed by a physical storage, be it a memory or a file. File mapped page cannot be decommitted in this sense, as it will be always backed by the file.


However, the code in the question mentioned is measuring the memory footprint, but what is measures is not representative, as the fact the page is removed from the process working set does not necessarily mean it is no longer present in a memory.

I have performed a different experiment, measuring how long it takes to read a byte from a memory mapped page. After unlocking the page or unmapping the view and closing the mapping handle the access was still fast.

For the access to be slow (i.e. to really discard the page from the memory) it was necessary to unmap the view and close BOTH memory mapping handle and file handle (the last was surprising to me, as I expected unmapping the view and closing the mapping handle will be enough).

It is still possible system will take VirtualUnlocked as a hint and it will discard the pages sooner, once it needs to discard something, but this is something I have to think about yet how to prove.



来源:https://stackoverflow.com/questions/3525202/how-can-i-decommit-a-file-mapped-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!