Jump to content
Sign in to follow this  

does SetLength for an array actually release memory?

Recommended Posts

Given code like this:

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

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

I peeked at the source code and it calls ReallocMem thus should work as expected.

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:


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