Home > Software engineering >  Memory usage is different between SetLength() and GetMem()?
Memory usage is different between SetLength() and GetMem()?

Time:10-27

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.

image

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.

image

Task Manager shows the correct memory usage when using SetLength(). My questions: Is SetLength() better than GetMem()?

This is my test:

Before

After

enter image description here

And SetLength shows similar figures. That is, as far as Delphi is considered, the memory usage is the same, as it should be.

  • Related