I use Delphi XE8 to test the SetLength() and GetMem() procedures.
Code to test SetLength():
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
a: array of Byte;
begin
SetLength(a,100*1024*1024); //100Mb
ShowMessage('See Task Manager');
SetLength(a,0);
end;
Task Manager shows the memory usage of Project1.exe is ~100Mb.
Code to test GetMem():
procedure TForm1.SpeedButton2Click(Sender: TObject);
var
p: PByte;
begin
GetMem(p,100*1024*1024); //100Mb
ShowMessage('See Task Manager');
FreeMem(p);
end;
Task Manager shows the memory usage of Project1.exe is ~2Mb.
Task Manager shows the correct memory usage when using SetLength(). My questions: Is SetLength() better than GetMem()?
This is my test:
After
And SetLength shows similar figures. That is, as far as Delphi is considered, the memory usage is the same, as it should be.




