This tip has been excerpted from Windows 2000 and Mainframe Integration, by Willian H. Zack, published by MacMillan Technical Publishing, 1999.
Program memory requests
By Hailey McKeefry
Ultimately, all Windows 2000 programs request the memory that they need using the Win32 (most often using the VirtualAlloc and VirtualFree API calls). VirtualAlloc is used to allocate a range of pages in the virtual address space of the calling process. In this API, parameters can be used to specify whether memory is completely committed or whether the pages are reserved for the processes' use. You can also specify a number of additional parameters, such as whether the allocated memory is read-write or read-only and whether writes to memory are cached. VirtualFree is used to release a range of pages in the virtual address space of the calling process. You can specify the starting address and the size of the page region to be freed, and can also decommit a range of pages without freeing them.
Windows 2000 allocates objects in memory from one of two memory pools: the paged pool, which is used for objects that can be paged into and out of memory; and the non-paged pool, which is used for system objects that cannot be paged out of memory.
Memory is allocated and used in stages:
- A range of memory addresses can be reserved by a certain process, without being physically allocated in a paging file. This speeds operations, since only in-memory page tables must be updated.
- Then, when the process requires it, the application commits the memory, which actually allocates the backup storage in a paging file.
- When a program finishes with the memory, it will free the memory so that the pages in the paging file and the page frames are released for reuse.
Ideally, the programs should use system memory in ways that preserve the resources of the system�allocating only the memory that it needs when it needs it and freeing the memory when it is through, so that memory requests and memory releases match exactly. Some programs, though, suffer from memory leak, in which the program claims memory without freeing it. Severe leaks will bring the program and even the operating system to its knees. When creating programs, make sure to leverage the memory management tools within the operating system effectively. Tracking down these leaks in applications and operating systems components after the fact is hugely difficult.
Hailey McKeefry is a contributing editor based in Belmont, Calif.
This was first published in August 2000