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 1-MB sections of address space, and commits memory as
required in 16-KB increments. It decommits and releases unused memory in 16-KB
and 1-MB 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.
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.
Share-memory manager (Windows only)
注意: Linuxs使用glibc函数如malloc进行内存管理。相关信息见Linux系统man页中的malloc。
在Windows系统中,内存管理程序管理应用程序中所有动态分配和释放的内存。New、Dispose、GetMem、ReallocMem、FreeMem等标准过程使用内存管理程序,并且所有对象和长串也是通过内存管理程序分配内存。
在Windows中,对于那些分配大量小到中块的应用程序来说,内存管理程序是最优化的,并且是典型的面向对象应用程序和处理串数据的应用程序。其他内存管理程序,如GlobalAlloc、LocalAlloc的实现,以及在Windows中私有堆支持,在相同条件下处理的不太好,而且如果直接使用将降低应用程序的效率。
为确保达到最佳性能,内存管理程序直接与Win32虚拟内存API(VirtualAlloc和VirtualFree函数)接口。内存管理程序保留了操作系统中地址空间的1-MB部分,并且以16-KB增量的请求提交内存。内存管理程序回收并释放在16-KB和1-MB部分中的内存。对于更小的块,提交的内存是更远的子分配。
内存管理程序块总是以4个字节为界向上舍入,并且总是包括4个字节的首部用于存储内存块的尺寸和其他状态位(bits)。这表明,内存管理程序块总是双字排列的(double-word-aligned),这在对块寻址时保证了最佳的CPU性能。
内存管理程序维持两个状态变量:AllocMemCount和AllocMemSize。它们包含了当前分配的内存块的数量和所有当前分配的内存块的尺寸之和。应用程序可以用这两个变量为调试显示状态信息。
System单元提供了两个过程,GetMemoryManager和SetMemoryManager,它们允许应用程序截获低级内存管理程序调用。System单元还提供了一个叫做GetHeapStatus的函数,它返回一个记录,该记录包含了详细的内存管理程序状态信息。