Chapter 2 Memory Management

2.1 About Memory

In Win32, each process has a unique 32-bit linear virtual address space that allows it to address up to 4 gigabytes of memory. The 2 GB in low memory are available to the user, and the 2 GB in high memory are reserved for the kernel. The virtual addresses used by a process do not represent the actual physical location of an object in memory. Instead, the kernel maintains a map for each process that translates virtual addresses into the corresponding physical addresses.

The virtual address space of each process is much larger than the total physical memory available to all processes. To increase the size of physical storage, the kernel uses the disk for backing storage. The total amount of storage available to all executing processes is the sum of physical memory (RAM) and the free space on disk available to the paging file. Physical storage and the virtual (or logical) address space of each process are organized into pages of 4 KB each. To maximize its flexibility in managing memory, the kernel can move the pages of physical memory to and from a paging file on disk. When a page is moved in physical memory, the kernel updates the page maps of the affected processes. Each process is guaranteed a minimum number of pages in physical memory, but the kernel has complete flexibility to move any page of memory to the paging file according to a least recently used algorithm. It is not possible for a process to lock a page so it cannot be swapped out to the paging file. Manipulation of physical memory by the kernel is completely transparent to applications, which deal only in their virtual address spaces.

The pages of a process' virtual address space can be in one of three states: free, reserved, or committed.

Free Free pages are those that are available to be committed or reserved.
Reserved A reserved page is one where the logical page has been allocated but no physical storage has been allocated for the page. The effect of this is to reserve a range of virtual addresses that then cannot be used by other allocation operations (i.e ., by malloc, LocalAlloc, etc.) without first being released. Attempting to read or write a free page or a reserved page results in a page fault exception. A reserved page may be freed or committed.
Committed A committed page is one for which physical storage (in memory or on disk) has been allocated. It may be protected to allow no access or read only access, or it may have read and write access. A committed page may be decommitted to release its storage, at which point it becomes a reserved page.