`
mywebcode
  • 浏览: 1000745 次
文章分类
社区版块
存档分类
最新评论

Chapter15-重置内存内容(Resetting the Contents of Physical Storage)

 
阅读更多
在正常的内存页面替换算法中,算法会优先选择将没被修改的页面替换出去,而让已修改的内存页面尽量久地留在内存中。并且,在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 <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
分享到:
评论

相关推荐

    App.Inventor.2.Building.Android.Apps.B0193RHQG6

    App Inventor 2 Building Android Apps takes you step-by-step through the whole process of designing and creating your first two android apps using the free MIT App Inventor 2 software. The book is ...

    2009 达内Unix学习笔记

    输出重定向,意思就是说,将原来屏幕输出变为文件输出,即将内容输到文件中。 输入重定向。 本来命令是通过键盘得到输入的,但是用小于号,就能够使命令从文件中得到输入。 \ 表示未写完,回车换行再继续。 * ...

    mysql-8-cookbook2018

    Chapter 2, Using MySQL, takes you through the basic uses of MySQL, such as creating databases and tables; inserting, updating, deleting, and selecting data in various ways; saving to different ...

    Professional C# 3rd Edition

    Chapter 15: Threading 439 Threading 439 Applications with Multiple Threads 441 Manipulating Threads 441 The ThreadPlayaround Sample 444 Thread Priorities 448 Synchronization 449 Summary 453 Chapter 16...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    - fixed output of the check boxes on the highlighted lines in PDF export - fixed bug with PDF anchors - fixed bug when using two or more macroses in memo version 4.12 --------------- + added support ...

    Active Directory Cookbook, 3rd Edition.pdf

    This chapter includes coverage of the new Fine-Grained Password Policy feature that was introduced in Windows Server 2008. Chapter 7, Groups Covers how to create groups, modify group scope and type, ...

    Winlicense v2.0.6.0

    WinLicense combines the same protection-level as Themida with the power of advanced license control, offering the most powerful and flexible technology that allows developers to securely distribute ...

    Agile Resetting and Restarting

    Agile Resetting and Restarting

    Genymotion_ARM_Translation-master.zip

    Resetting the Emulator Install Adb brew cask install android-platform-tools Samples and Common Problems An error occured while deploying the file. This probably means that the app contains ARM ...

    Research on six-degree-of-freedom calibration system for wind tunnel balances with a collimated laser beam

    The system is composed of four parts: the automatically loading subsystem, the automatically resetting subsystem, the data-acquisition subsystem and the measurement subsystem. The results of some ...

    Mockito Essentials(PACKT,2014)

    By sequentially working through the steps in each chapter, you will quickly learn the features of Mockito. Mockito Essentials will ensure your success with these concepts, tools, and frameworks. ...

    xasdk-3.2.8c

    + Fixed bug in resetting the Equalizer to NULL * What's new in 3.0.3: + New DirectSound sample code for Delphi + Removed dependency to MsgWaitForMutipleObjectsEx * What's new in 3.0.2: + Fixed ...

    nike-account-resetter:耐克帐户工具; 支持发送重置电子邮件,重置nike帐户,删除看到的重置电子邮件

    Nike帐户重置者@ ayyitsc9 主要功能:通过为重置电子邮件抓取您的电子邮件来重置nike帐户(全自动) 次要功能:将Nike重置电子邮件发送到emails_to_send_to.txt中的电子邮件 第三级功能:删除已读的Nike重置电子...

    Difference-Based Partial Reconfiguration

    Instead of resetting the device and performing a complete reconfiguration, new data is loaded to reconfigure a specific area of a device, while the rest of the device is still in operation.

    Utilities for Windows NT 源码

    Subsequent users have the choice of resetting the PC or calling an administrator who can unlock the workstation. While lots of Windows NT settings can be changed through the registry, disabling the ...

    Resetting, Checking Out & Reverting | git里边容易混淆的几个命令解释

    附件里边是一个链接,尝试保存pdf下来,但是格式总是不对,所以只能把链接放上来了

    EurekaLog_7.5.0.0_Enterprise

    7)....Added: Streaming unpacked debug info into temporal files instead of memory - this greatly reduces run-time application memory usage at cost of slightly slower exception processing. This also ...

    Moodle Administration Essentials(PACKT,2015)

    You'll learn to create user accounts and understand the methods of authentication based on manual accounts and e-mail-based self-registrations. You'll then develop the Moodle site structure and course...

    Fast Report Enterprise v4.9.32 Full Source

    - fixed resetting of Page variable in double-pass report with TfrxCrossView - fixed bug with loosing of aligning when split TfrxRichView - fixed buzz in reports with TfrxRichView when using RTF 4.1

    FastReport 4.9

    - fixed resetting of Page variable in double-pass report with TfrxCrossView - fixed bug with loosing of aligning when split TfrxRichView - fixed buzz in reports with TfrxRichView when using RTF 4.1

Global site tag (gtag.js) - Google Analytics