Hi all,
I am wondering why my program needs more memory the longer it runs - well it should not. I assume that I am doing something wrong with the dynamic arrays I'm using...
I've got a fixed array of dynamic arrays holding records,
e.g.:
My program starts at position 0 of the Items array and assigns to it a certain number of TReservation records (normally 1 to 4), and that way it continues until position 99, clearing the previous items. Then it starts to re-use position 0 and so on... just like a ring buffer.
I thought this could work that way (schematically):
I know that I would not have to clear all the previous items right away in this code sample, but this is only a scheme and my 'real' program needs to really clear previous items.
I am assuming that Finalize() or SetLength(x,0) do not free the memory assigned to the dynamic array with the records.
Keep in mind that I could assign 5 records to position 0 at the first run, and when I return to position 0 I could assign zero records. Then the memory for the 5 previously assigned records should be freed.
What am I doing wrong, or am I doing everything right?
Thanks for any suggestions!
Anne
I am wondering why my program needs more memory the longer it runs - well it should not. I assume that I am doing something wrong with the dynamic arrays I'm using...
I've got a fixed array of dynamic arrays holding records,
e.g.:
Code:
TReservation = record
Directed: Boolean;
Source, Destination: TStationAddress;
// these are only pointers to already existing objects
Priority: 0..15;
TimeOfRequest: Cardinal;
end;
TReservations = array of TReservation;
TReservationTable = class(TObject)
private
Items: array[0..99] of TReservations;
end;
My program starts at position 0 of the Items array and assigns to it a certain number of TReservation records (normally 1 to 4), and that way it continues until position 99, clearing the previous items. Then it starts to re-use position 0 and so on... just like a ring buffer.
I thought this could work that way (schematically):
Code:
var
i, j, CurrentPosition: Integer;
for i := 0 to 99999 do
begin
CurrentPosition := i mod 100;
// clear the item before the current item
Finalize(Items[(i-1) mod 100]);
// OR (which one is better?)
SetLength(Items[(i-1) mod 100], 0);
// save some data into the current item
SetLength(Items[CurrentItem], RandomRange(1,5));
for j := 0 to High(Items[CurrentItem]) do
begin
Items[CurrentItem][j].Directed := True;
Items[CurrentItem][j].Priority := 12;
// ...
end;
end;
I know that I would not have to clear all the previous items right away in this code sample, but this is only a scheme and my 'real' program needs to really clear previous items.
I am assuming that Finalize() or SetLength(x,0) do not free the memory assigned to the dynamic array with the records.
Keep in mind that I could assign 5 records to position 0 at the first run, and when I return to position 0 I could assign zero records. Then the memory for the 5 previously assigned records should be freed.
What am I doing wrong, or am I doing everything right?
Thanks for any suggestions!
Anne