Chapter15-重置内存内容(Resetting the Contents of Physical Storage)
发布日期:2021-10-22 10:56:45 浏览次数:6 分类:技术文章

本文共 3542 字,大约阅读时间需要 11 分钟。

     在正常的内存页面替换算法中,算法会优先选择将没被修改的页面替换出去,而让已修改的内存页面尽量久地留在内存中。并且,在windows系统下,当替换算法不得已要将一个已修改的页面替换出 RAM 时,系统还会先将替换的已修改页面备份到磁盘的分页文件(Paging file)中去(方便以后再次读取),而这个过程是比较慢的。
     重置内存(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 
#include
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核心编程》时的一些学习笔记,有疏忽之处,欢迎各位网友指正。QQ邮箱:job.zhanghui@qq.com

转载于:https://www.cnblogs.com/java20130722/archive/2013/01/29/3207099.html

转载地址:https://blog.csdn.net/weixin_30326745/article/details/95637245 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:js中的null 和undefined
下一篇:无法下载APP

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月29日 18时33分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章