The VirtualAlloc Function: Committed and Reserved Memory
If your program needs dynamic memory, sooner or later the Win32 VirtualAlloc function will be called. Chances are that your program will never call VirtualAlloc; instead, you'll rely on the Windows heap or the CRT heap functions to call it directly. Knowing how VirtualAlloc works, however, will help you better understand the functions that call it.First, you must understand what reserved and committed memory are. When memory is reserved, a contiguous virtual address range is set aside. If, for example, you know that your program is going to use a single memory block (known as a region) that is 5 MB in size but you don't need to use it all right away, you can call VirtualAlloc with a MEM_RESERVE allocation type parameter and a 5 MB size parameter. Windows will round the start address of the region to a 64 KB boundary and prevent your process from reserving other memory in the same range. You can specify a start address for your region, but more often you'll let Windows assign it for you. Nothing else will happen. No RAM will be allocated, and no swap file space will be set aside.
When you get more serious about needing memory, you can call VirtualAlloc again to commit the reserved memory, using a MEM_COMMIT allocation type parameter. Now the start and end addresses of the region will be rounded to 4 KB boundaries, and corresponding swap file pages will be set aside together with the required page table. The block will be designated as either read-only or read/write. Still, no RAM will be allocated, however; RAM allocation occurs only when you try to access the memory. If the memory was not previously reserved, no problem. If the memory was previously committed, still no problem. The rule is that memory must be committed before you can use it.You can call the VirtualFree function to "decommit" committed memory, thereby returning the designated pages back to reserved status. VirtualFree can also free a reserved region of memory, but you have to specify the base address you got from a previous VirtualAlloc reservation call.