var
  si: TSystemInfo;
begin
  GetSystemInfo (si);
  ShowMessageFmt ('% p-% p', [si.lpMinimumApplicationAddress, si.lpMaximumApplicationAddress]);
   {Result is: 00010000-7FFEFFFF}
end ;


An important memory-related parameter can also be obtained through GetSystemInfo: PageSize

var
  si: TSystemInfo;
begin
  GetSystemInfo (si);
  ShowMessage (IntToStr (si.dwPageSize)); {4096; 4096 bytes is 4K}
end ;


PageSize is the basic unit of system management memory. The reason why you need to use GetSystemInfo to get the PageSize of different systems will be different.

What we need to know is that the memory allocated using the VirtualAlloc function is based on PageSize (4K) as the smallest unit; if we use VirtualAlloc allocates memory to an integer (4 bytes), which will waste 4092 bytes, which means that VirtualAlloc is not suitable for allocating small memory, so there are multiple functions for allocating memory.

Let’s put this topic aside for a while, first understand the “stack”.

When it comes to “stack”, we think of “heap”, and “stack” also refers to “stack”; “stack” and “heap” are program-operable memory A certain segment in the area ($ 10000-$ FFEFFFF).

There are HeapReAlloc, GlobalAlloc, and other functions that allocate a “heap” in the system functions, but there is no function that allocates a “stack”. This is because the “stack” is automatically managed by the program; each Each program leaves a “stack” from its available address range. The program can automatically adjust its size as needed, but we can set its maximum and minimum values. In Delphi, you can set it from here:
Project- > Options-> Linker-> [Min stack size and Max stack size]

“Stack” is used to temporarily store local variables and function parameters, which are applied by the program when needed, and released after use.

Because the space of “stack” is generally not very large Large, so we generally don’t make local variables too large (especially when using arrays);
because accessing the “stack” is more concise and faster than accessing the “heap”, it is necessary to use more local variables and less global variable.