[英]How to release physical memory when using memory mapped files?
I have this test code whic is simply mapping about 7500 files to virtual memory and then unmapping it. 我有这个测试代码,只是将约7500个文件映射到虚拟内存,然后取消映射。 I have done this to check my RAM usage (my virtual memory usage is just fine).
我这样做是为了检查我的RAM使用情况(我的虚拟内存使用情况还可以)。 When I run this application my physical avaialble memory goes dangerously low and everything really slows down (I am running on a 4 GB RAM machine XP 32 bit SP3).
当我运行该应用程序时,我的物理可用内存变得非常危险,并且一切实际上都变慢了(我在4 GB RAM计算机XP 32位SP3上运行)。 Does any body know how can I solve this ?
有谁知道我该如何解决? ( I searched and found that
VirtualUnlock
may be useful, but when I tried it didn't make any difference). (我搜索发现
VirtualUnlock
可能有用,但是当我尝试时它没有任何区别)。 Here is the code: 这是代码:
int COUNT = 7759;
cout<<"Press a key to start\n";
getchar();
int i = 1;
for(i = 1; i <= COUNT; ++i)
{
CString s;
s.Format(_T("E:\\Images\\CD5\\04221155\\2 (%d)"), i);
HANDLE hFile = CreateFile(s,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
TCHAR buf[256] = {0};
DWORD er = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, er, 0, buf, 256, 0);
cout<<"Unable to open file, Error is:";
wcout<<buf;
cout<<"\n";
break;
}
DWORD filesize = 0;
filesize = GetFileSize(hFile,&filesize);
HANDLE hMapFile = CreateFileMapping(hFile,
NULL,
PAGE_READWRITE,
0,
filesize,
NULL);
if(hMapFile == NULL)
{
cout<<"Unable to create file mapping object. Error is:";
TCHAR buf[256] = {0};
DWORD er = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, er, 0, buf, 256, 0);
wcout<<buf;
cout<<"\n";
break;
}
char* pBuf = (char*) MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
filesize);
if(pBuf == NULL)
{
TCHAR buf[256] = {0};
DWORD er = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, er, 0, buf, 256, 0);
cout<<"Unable to map the file to virtual memory, Error is:";
wcout<<buf;
CString s1;
s1.Format(_T("%x"), hMapFile);
cout<<"Handle is:"<<s1;
cout<<"\n";
break;
}
if(i % 100 == 0)
{
cout<<"Mapped "<<i<<"\n";
}
memset(pBuf, 0, filesize);
//VirtualUnlock(pBuf, filesize);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
CloseHandle(hFile);
}
cout<<"finished i is "<<i<<"\n";
getchar();
You are using MMFs in the absolute worst possible way. 您以绝对最差的方式使用MMF。 Mapping them once, hitting every page and immediately closing the mapping.
一次映射它们,点击每个页面并立即关闭映射。 Your program is generating a massive number of page faults.
您的程序正在生成大量的页面错误。 Something you can easily see in Taskmgr.exe, Processes tab, View + Select Columns to add the page fault columns.
您可以在Taskmgr.exe的“进程”选项卡的“查看+选择列”中轻松添加一些内容,以添加页面错误列。 Yes, page faults are fairly expensive in getting so many of them is going to noticeably affect the machine's operation.
是的,页面错误的代价非常高昂,因为要使很多页面缺陷明显影响机器的运行。 RAM is par for the course, you are using a lot of it.
RAM是该课程的标准配置,您正在使用很多。
I realize this is a synthetic test. 我意识到这是一个综合测试。 If your real code looks anything like this (never taking advantage of the caching and the lazy writeback) then don't use MMFs.
如果您的真实代码看起来像这样(不要利用缓存和延迟写回),则不要使用MMF。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.