There is some pattern in memory allocation (Windows 11, FPC 3.2.2).
Program Example;
{ Program to demonstrate the FreeMem and GetMem functions. }
Var
P : Pointer;
MM : Longint;
F: PtrUInt;
i: Integer;
{ Effectively used memory for aSize for small blocks. From BrunoK }
function CalculatedChunkSize(aSize:UIntPtr):UIntPtr;
begin
Result := ((SizeOf(UIntPtr) + aSize + (Sizeof(UIntPtr) * 4) - 1)
div (Sizeof(UIntPtr) * 4)) *
(Sizeof(UIntPtr) * 4);
end;
begin
for i := 1 to 12 do
begin
MM := i * 8;
write('MM = ', MM);
P := GetMem(MM);
writeln(' MemSize(P) = ', MemSize(P));
write('SizeOf(Pointer) = ', SizeOf(Pointer));
// From Jamie
writeln(' MemSize(P)-SizeOf(Pointer) = ', MemSize(P) - SizeOf(Pointer));
F := FreeMem(P);
write('FreeMem(P) = ', F);
// From BrunoK
writeln(' CalculatedChunkSize(MM) = ', CalculatedChunkSize(MM));
end;
readln;
end.
MM = 8 MemSize(P) = 24
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 16
FreeMem(P) = 32 CalculatedChunkSize(MM) = 32
MM = 16 MemSize(P) = 24
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 16
FreeMem(P) = 32 CalculatedChunkSize(MM) = 32
MM = 24 MemSize(P) = 24
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 16
FreeMem(P) = 32 CalculatedChunkSize(MM) = 32
MM = 32 MemSize(P) = 56
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 48
FreeMem(P) = 64 CalculatedChunkSize(MM) = 64
MM = 40 MemSize(P) = 56
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 48
FreeMem(P) = 64 CalculatedChunkSize(MM) = 64
MM = 48 MemSize(P) = 56
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 48
FreeMem(P) = 64 CalculatedChunkSize(MM) = 64
MM = 56 MemSize(P) = 56
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 48
FreeMem(P) = 64 CalculatedChunkSize(MM) = 64
MM = 64 MemSize(P) = 88
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 80
FreeMem(P) = 96 CalculatedChunkSize(MM) = 96
MM = 72 MemSize(P) = 88
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 80
FreeMem(P) = 96 CalculatedChunkSize(MM) = 96
MM = 80 MemSize(P) = 88
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 80
FreeMem(P) = 96 CalculatedChunkSize(MM) = 96
MM = 88 MemSize(P) = 88
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 80
FreeMem(P) = 96 CalculatedChunkSize(MM) = 96
MM = 96 MemSize(P) = 120
SizeOf(Pointer) = 8 MemSize(P)-SizeOf(Pointer) = 112
FreeMem(P) = 128 CalculatedChunkSize(MM) = 128