dummzeuch 1505 Posted August 5, 2022 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 Posted August 5, 2022 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
Lajos Juhász 293 Posted August 5, 2022 I peeked at the source code and it calls ReallocMem thus should work as expected. Share this post Link to post
Stefan Glienke 2006 Posted August 5, 2022 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 1 1 Share this post Link to post