Skip to content

Instantly share code, notes, and snippets.

@kevroletin
Last active December 14, 2015 10:18
Show Gist options
  • Save kevroletin/5070644 to your computer and use it in GitHub Desktop.
Save kevroletin/5070644 to your computer and use it in GitHub Desktop.
Explore how many memory Delphi allocates for FrameObjects.
program frameobj_alloc;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
const
delim : string = ': ';
var
initAlocation: Integer;
lastAllocation: Integer = 0;
procedure PrintMemUsage(i: Integer); overload;
var value: Integer;
begin
value := GetHeapStatus.TotalAllocated;
Writeln( i, delim, value - initAlocation, ' + ', value - lastAllocation );
lastAllocation := value;
end;
procedure CheckNested;
var a: array[1..10000] of Integer; // captured
b: array[1..10000] of Integer;
function Sub1: TProc;
var b: array[1..10000] of Integer;
function Sub2: TProc;
function Sub3: TProc;
function Sub4: TProc;
begin
PrintMemUsage(40);
Result := procedure begin
b[1] := 0;
a[1] := 0;
end;
end;
begin
PrintMemUsage(30);
Result := Sub4();
end;
begin
PrintMemUsage(20);
Result := Sub3();
end;
begin
PrintMemUsage(10);
Result := Sub2();
end;
var f: TProc;
begin
PrintMemUsage(1);
f := Sub1();
PrintMemUsage(2);
f();
f := nil;
PrintMemUsage(3);
end;
begin
initAlocation := GetHeapStatus.TotalAllocated;
lastAllocation := initAlocation;
CheckNested;
Readln;
end.
@kevroletin
Copy link
Author

Result of execution:

1: 40236 + 40236
10: 80472 + 40236
20: 80500 + 28
30: 80528 + 28
40: 80556 + 28
2: 80556 + 0
3: 80556 + 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment