I have several large dynamic 3D arrays as "array of array of array of integer;" or "of TSomeObject", etc. Not too much, but most of them are temporary. So after execution of main algorithm procedure I try to "free" them, as they are variables in a class, and therefore would be freed only after class destructor.So I try to free them this way (just the reverse way they are created):

is enough to free all the memory of the dynamically allocated array? I make sure that all specific object references (if any) are freed before setting the length of the array to zero.This matters significantly, as this procedure is "duplicated" in a destructor, which might (under some idiotic conditions) fire on half-freed or half-created array, so trying to access nmap[ix,iy] would lead to a error.

Initialize and Finalize are for variables allocated with GetMem. If the dynamic array was declared in a var block (in a function, or in the interface or implementation section) its heap memory is managed automatically.

As for the original question, they are variables in a class. Both code snippets should be equivalent, provided that beforehand they are traversed entirely to free the objects they contain. For other types like integers or strings, you can set the length of the 1st dimension of the array to 0 safely, the compiler will do everything for you. I'd say that both code snippets take approximately the same amount of time. I'm using dynamic arrays constantly and never had a memory leak due to them. Only with objects for now, when I mess up about when I am freeing them or not.

Wargan, jmm72 thanks a lot!"set the length of the 1st dimension of the array to 0 safely" should do it.

P.S. I wasn't talking about memory leaks, but the situation is the following:

I generate a game map in a thread (let's those be 8 threads).At some point threads peak their memory consumptions (when checking for map consistency).Eventually they finish and stop waiting until other code (a generation manager) "takes" and "saves" the maps.During this time more threads can start which will also require some memory.So, just to minimize the memory use I can "free" some of the temporary arrays, so that RAM frees up for next processes.e.g. the cycle looks like:

Generation class createInit parametersStart threadMake temporary arrays (use a lot of RAM in case the map is big )Process the mapFree temporary arrays (free those megabytes)Halt and wait for the data to be copiedExternal manager frees the generation class