0

I want to allocate a chunk of memory in Windows and be sure that it will get no #GP or #PF faults. Regarding #GP, it's my responsibility as a programmer to ensure that I do not exceed any bounds. However, #PF are the the responsibility of the OS, since it can choose whether or not to evict a page.

I imagine that if I use the same page frequently, the OS will be smart enough not to evict it. However, if I want to allocate a large block of memory, then it'll take me a while to reach some of the pages, and I don't want the OS to evict it in the meanwhile.

Is there any way to tell Windows to keep a page present so that I never get a page fault?

ote: This is similar to to the question How can I tell Linux to keep a page and not evict it? except that this one is about Windows

Community
  • 1
  • 1
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • This is inappropriate, only device drivers should ever worry about this. You should allow the operating system to determine when RAM is needed for another process or the file system cache. These are not decisions you can possibly make correctly yourself. You can use VirtualLock but the quota for locked pages is small. – Hans Passant Aug 30 '11 at 11:56
  • @Hans: This is not for production code. It is for a profiler I'm building for my MSc. IT WILL NOT BE SHIPPED TO ANYBODY ANYTIME! – Nathan Fellman Aug 30 '11 at 13:33

1 Answers1

2

Have a look at the VirtualLock function:

Locks the specified region of the process's virtual address space into physical memory, ensuring that subsequent access to the region will not incur a page fault.

There's an example in this page: Creating Guard Pages.

Mat
  • 202,337
  • 40
  • 393
  • 406