Quick start manual

Memory management
11-1
Chapter
11
Chapter11
Memory management
This chapter explains how programs use memory and describes the internal formats
of Delphi data types.
The memory manager (Windows only)
Note
Linux uses glibc functions such as malloc for memory management. For information,
refer to the malloc man page on your Linux system.
On Windows systems, the memory manager manages all dynamic memory
allocations and deallocations in an application. The New, Dispose, GetMem,
ReallocMem, and FreeMem standard procedures use the memory manager, and all
objects and long strings are allocated through the memory manager.
On Windows, the memory manager is optimized for applications that allocate large
numbers of small- to medium-sized blocks, as is typical for object-oriented
applications and applications that process string data. Other memory managers, such
as the implementations of GlobalAlloc, LocalAlloc, and private heap support in
Windows, typically do not perform well in such situations, and would slow down an
application if they were used directly.
To ensure the best performance, the memory manager interfaces directly with the
Win32 virtual memory API (the VirtualAlloc and VirtualFree functions). The memory
manager reserves memory from the operating system in 1Mb sections of address
space, and commits memory as required in 16K increments. It decommits and
releases unused memory in 16K and 1Mb sections. For smaller blocks, committed
memory is further suballocated.
Memory manager blocks are always rounded upward to a 4-byte boundary, and
always include a 4-byte header in which the size of the block and other status bits are
stored. This means that memory manager blocks are always double-word-aligned,
which guarantees optimal CPU performance when addressing the block.