In the Windows memory-management system, your application can allocate blocks of memory, called memory objects. You can allocate memory objects from either the global or the local heap. The global heap is a pool of free memory available to all applications. The local heap is a pool of free memory available to just your application. In managing the system memory, Windows also manages the code and data segments of your application.
In some memory-management systems, the memory you allocate remains fixed at a specific memory location until you free it. In Windows, allocated memory can be also be movable and discardable.
A movable memory object does not have a fixed address; Windows can move it at any time to a new address. Movable memory objects let Windows make the best use of free memory. For example, if a movable memory object separates two free memory objects, Windows can move the movable object to combine the free objects into one contiguous object.
Discardable memory is similar to movable memory in that Windows can move it, but Windows can also reallocate a discardable object to zero length if it must use the space to satisfy an allocation request. Reallocating a memory object to zero length destroys the data the object contains, but an application always has the option of reloading the discarded data whenever it is needed.
When you allocate a memory object, you receive a handle, rather than a pointer, to that memory object. The handle identifies the allocated object. You use it to retrieve the object's current address when you need to access the memory.
To access a memory object, you lock the memory handle. This temporarily fixes the memory object and returns a pointer to its beginning. While a memory handle is locked, Windows cannot move or discard the object. Therefore, after you have finished using the object, you should unlock the handle as soon as possible. Keeping a memory handle locked makes Windows memory management less efficient and can cause subsequent allocation requests to fail.
Windows lets you compact memory. By “squeezing” the free memory from between allocated memory objects, Windows collects the largest contiguous free memory object possible, from which you may allocate additional memory objects. This squeezing is a process of moving and (if necessary) discarding memory objects. You can also discard individual memory objects if you temporarily have no need for them.