简体   繁体   English

内存映射文件是否保留在物理内存中?

[英]Memory-mapped files remain in physical memory?

I have a process that uses a lot of memory mapped files. 我有一个使用大量内存映射文件的进程。
Problem is that those files are kept in physical memory, even when the machine is low on memory, and other processes require this memory. 问题是这些文件保存在物理内存中,即使机器内存不足,其他进程也需要此内存。

I've tried using SetProcessWorkingSetSize to limit the process working set, but it doesn't help, the process' working set keeps growing over the max value. 我已经尝试使用SetProcessWorkingSetSize来限制进程工作集,但它没有帮助,进程的工作集不断增长超过最大值。

Is there a better way to limit the process' working set? 有没有更好的方法来限制流程的工作集?
Can I change Windows' heuristcs for paging memory mapped files? 我可以更改Windows的heuristcs用于分页内存映射文件吗?

If you find that your process with the memory mapped files is holding on to a lot of those pages, then that means the OS has not needed to discard any of your memory mapped regions to give to other processes. 如果您发现使用内存映射文件的进程持有很多这些页面,那么这意味着操作系统不需要丢弃任何内存映射区域以提供给其他进程。 So, how do you know other processes actually need the memory currently used for the mapped files? 那么,您如何知道其他进程实际上需要当前用于映射文件的内存? Just because the OS is low on physical RAM means nothing. 仅仅因为操作系统物理RAM较低就没有任何意义。 Other processes have to demand memory to cause the OS to drop your mapped pages and give RAM to them. 其他进程必须要求内存使操作系统删除映射的页面并为它们提供RAM。

So, it appears that your mmap-I/O process is starving your other process which uses RAM less frequently. 因此,您的mmap-I / O进程似乎正在使您的其他进程不再使用RAM。 One approach is to judiciously lock memory in the process that is being starved. 一种方法是明智地锁定正在饥饿的进程中的内存。 Look at VirtualLock for win32. 查看VirtualLock for win32。

Ended up using brute force VirtualUnlock. 使用强力VirtualUnlock结束。

PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
        if (pmc.WorkingSetSize > MaxWorkingSetSize)
        {
                VirtualUnlock(FilePtr.pData, MaxWorkingSetSize);
                UnmapViewOfFile(FilePtr.pData);
                CloseHandle(FilePtr.hFileMap);
                CloseHandle(FilePtr.hFile);
        }
}

I think this behaviour is due to how MMF (Memory Mapped Files) work. 我认为这种行为是由于MMF(内存映射文件)的工作原理。 Have a look at this blog article. 看看这篇博客文章。 It explains that MMF files skip the Windows paging process, and thus are not backed by the page file. 它解释了MMF文件跳过Windows分页过程,因此不受页面文件的支持。 Instead, the MMF becomes the data backup in itself, which means, ultimately, it takes up more RAM since it's not being pages out (Er, I'm not sure I get that myself - best to read the blog!) 相反,MMF本身就成了数据备份,这意味着,最终它会占用更多的RAM,因为它不是页面输出(呃,我不确定自己能不能这样做 - 最好阅读博客!)

Here's the MSDN documentation for memory mapped files and here's another SO question relating to MMF. 这是内存映射文件的MSDN文档, 这是与MMF相关另一个SO问题。

Check my answer here ; 在这里检查我的答案; with VirtualUnlock() you can manually uncommit portions of the MMFs; 使用VirtualUnlock(),您可以手动取消提交部分MMF; eg, sections you don't think you'll be accessing again soon. 例如,您认为不会很快再次访问的部分。

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

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