memory reserving and committing memory reserving and committing windows windows

memory reserving and committing


The whole point of reserving pages is to ensure that contiguous address space is available for some task. For example, we want the stack to able to grow to 1MB, but we don't want to commit all of that memory because it won't actually be used yet. So we reserve 1MB of pages, but commit a small amount, like 64kB. By setting up a guard page at the end of the committed region, we can detect when we need to commit more memory.

Committing memory is the act of mapping some kind of storage to a page. This can be located in physical RAM, where it is part of the working set, or in the pagefile. It can also be mapped in or private memory. NtAllocateVirtualMemory/VirtualAlloc can reserve and commit at the same time, for convenience.

EDIT for updated question: When you commit pages, that is charged against the process pagefile quota / system-wide commitment limit. This limit is determined by the amount of physical RAM available and the size of the pagefile. This doesn't actually mean the pages are stored in, or written to the pagefile. They may be if memory is low, but otherwise pages are mostly kept in physical memory.


  • You don't actually have to follow 2-stage reserve/commit allocation scheme.

    The point is that VirtualAlloc and VirtualFree may do several things. And sometimes it's really useful to do so. You don't have to however.

  • Committed memory region is the one for which the system allocates physical storage.

    You don't have to worry about where exactly it's allocated: RAM or page file. This should be transparent to you (unless you're writing a kernel-mode device driver). Moreover, most of the memory pages can be swapped out to the page file and loaded into RAM on-demand.

    It's just some committed memory pages don't require binding with the page file, because they're already bound to another physical storage. Memory-mapped files are the example of this.

    In normal scenario when you commit a memory pages, use it for some time period and free it - most likely it won't reach the page file at all.