Jump to content
Sign in to follow this  
dummzeuch

does SetLength for an array actually release memory?

Recommended Posts

Given code like this:

var
  arr: array of TSomeLargeRecord;
begin
  SetLength(arr, 5000);
  // fill the array
  SetLength(arr, 100);
  // work with the smaller array
end;

Does the last call to SetLength actually reduce the memory required for the array?

 

I'm suspecting that it only reduces the internally stored capacity but the memory will remain allocated until the array is finalized, that is it goes out of scope, is set to NIL or SetLength(arr,0) is called.

Share this post


Link to post
Guest

According to the documentation, it should:
 

Quote

For a dynamic array variable, SetLength reallocates the array referenced by S to the given length.

 

Share this post


Link to post

That's up to the implementation of ReallocMem - the one that comes with Delphi (FastMM4) (I just looked into getmem.inc) has this comment:

Quote

It's a downsize. Do we need to allocate a smaller block? Only if the new size is less than a quarter of the available size less SmallBlockDownsizeCheckAdder bytes

 

  • Like 1
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×