在正常的内存页面替换算法中,算法会优先选择将没被修改的页面替换出去,而让已修改的内存页面尽量久地留在内存中。并且,在windows系统下,当替换算法不得已要将一个已修改的页面替换出 RAM 时,系统还会先将替换的已修改页面备份到磁盘的分页文件(Paging file)中去(方便以后再次读取),而这个过程是比较慢的。
重置内存(Resetting storage)的意思就是主动告知系统:“这些页面没被修改过,你要替换它们时,不用备份到分页文件中去了。”
重置内存(Resetting storage)可以调用 VirtualAlloc 函数实现,需要将第三个参数设置为 MEM_RESET ;第一参数当然是需要重置区域的开始地址(要按照系统页面大小对齐);第二个参数是重置区域的大小(以字节为单位,且必须是页面大小的整数倍);第四个参数就对应的重置区域的保护属性了。
重置内存(Resetting storage)的意思就是主动告知系统:“这些页面没被修改过,你要替换它们时,不用备份到分页文件中去了。”
重置内存(Resetting storage)可以调用 VirtualAlloc 函数实现,需要将第三个参数设置为 MEM_RESET ;第一参数当然是需要重置区域的开始地址(要按照系统页面大小对齐);第二个参数是重置区域的大小(以字节为单位,且必须是页面大小的整数倍);第四个参数就对应的重置区域的保护属性了。
LPVOID WINAPI VirtualAlloc(
__in LPVOID lpAddress,
__in SIZE_T dwSize,
__in DWORD flAllocationType,
__in DWORD flProtect
);
在重置内存区域的时候,有两种可能:
- 当区域页面已经在分页文件中时,表示该页面已经被替换出去了。调用 VirtualAlloc 函数重置后,系统会直接删除对应的页面。
- 当区域页面还在RAM中时,调用 VirtualAlloc 函数重置后,页面都会被标记为"未修改",这样它们被替换出去的时候就不会被写到分页文件中去了。
下面是《windows核心编程》中的一个示例:
#include <tchar.h>
#include <Windows.h>
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, PTSTR, int) {
TCHAR szAppName[] = TEXT("MEM_RESET tester");
TCHAR szTestData[] = TEXT("Some text data");
// Commit a page of storage and modify its contents.
PTSTR pszData = (PTSTR) VirtualAlloc(NULL, 1024,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
_tcscpy_s(pszData, 1024, szTestData);
if (MessageBox(NULL, TEXT("Do you want to access this data later?"),
szAppName, MB_YESNO) == IDNO) {
// We want this page of storage to remain in our process but the
// contents aren't important to us anymore.
// Tell the system that the data is not modified.
// Note: Because MEM_RESET destroys data, VirtualAlloc rounds
// the base address and size parameters to their safest range.
// Here is an example:
// VirtualAlloc(pvData, 5000, MEM_RESET, PAGE_READWRITE)
// resets 0 pages on CPUs where the page size is greater than 4 KB
// and resets 1 page on CPUs with a 4 KB page. So that our call to
// VirtualAlloc to reset memory below always succeeds, VirtualQuery
// is called first to get the exact region size.
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery(pszData, &mbi, sizeof(mbi));
VirtualAlloc(pszData, mbi.RegionSize, MEM_RESET, PAGE_READWRITE);
}
// Commit as much storage as there is physical RAM.
MEMORYSTATUS mst;
GlobalMemoryStatus(&mst);
PVOID pvDummy = VirtualAlloc(NULL, mst.dwTotalPhys,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
// Touch all the pages in the dummy region so that any
// modified pages in RAM are written to the paging file.
if (pvDummy != NULL)
ZeroMemory(pvDummy, mst.dwTotalPhys);
// Compare our data page with what we originally wrote there.
if (_tcscmp(pszData, szTestData) == 0) {
// The data in the page matches what we originally put there.
// ZeroMemory forced our page to be written to the paging file.
MessageBox(NULL, TEXT("Modified data page was saved."),
szAppName, MB_OK);
} else {
// The data in the page does NOT match what we originally put there
// ZeroMemory didn't cause our page to be written to the paging file
MessageBox(NULL, TEXT("Modified data page was NOT saved."),
szAppName, MB_OK);
}
// Don't forget to release part of the address space.
// Note that it is not mandatory here since the application is exiting.
if (pvDummy != NULL)
VirtualFree(pvDummy, 0, MEM_RELEASE);
VirtualFree(pszData, 0, MEM_RELEASE);
return(0);
}
《windows核心编程》(笔记)
系列文章是本人看《windows核心编程》时的一些学习笔记,有疏忽之处,欢迎各位网友指正。QQ邮箱:job.zhanghui@qq.com