Quick start manual

11-2
Delphi Language Guide
The memory manager (Windows only)
The memory manager maintains two status variables, AllocMemCount and
AllocMemSize, which contain the number of currently allocated memory blocks and
the combined size of all currently allocated memory blocks. Applications can use
these variables to display status information for debugging.
The System unit provides two procedures, GetMemoryManager and
SetMemoryManager, that allow applications to intercept low-level memory manager
calls. The System unit also provides a function called GetHeapStatus that returns a
record containing detailed memory-manager status information. For further
information about these routines, see the online Help.
Variables
Global variables are allocated on the application data segment and persist for the
duration of the program. Local variables (declared within procedures and functions)
reside in an application’s stack. Each time a procedure or function is called, it
allocates a set of local variables; on exit, the local variables are disposed of. Compiler
optimization may eliminate variables earlier.
Note
On Linux, stack size is set by the environment only.
On Windows, an application’s stack is defined by two values: the minimum stack size
and the maximum stack size. The values are controlled through the $MINSTACKSIZE
and $MAXSTACKSIZE compiler directives, and default to 16,384 (16K) and
1,048,576 (1Mb) respectively. An application is guaranteed to have the minimum
stack size available, and an application’s stack is never allowed to grow larger than
the maximum stack size. If there is not enough memory available to satisfy an
application’s minimum stack requirement, Windows will report an error upon
attempting to start the application.
If a Windows application requires more stack space than specified by the minimum
stack size, additional memory is automatically allocated in 4K increments. If
allocation of additional stack space fails, either because more memory is not available
or because the total size of the stack would exceed the maximum stack size, an
EStackOverflow exception is raised. (Stack overflow checking is completely automatic.
The $S compiler directive, which originally controlled overflow checking, is
maintained for backward compatibility.)
On Windows or Linux, dynamic variables created with the GetMem or New
procedure are heap-allocated and persist until they are deallocated with FreeMem or
Dispose.
Long strings, wide strings, dynamic arrays, variants, and interfaces are heap-
allocated, but their memory is managed automatically.