Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic arrays, SetLength and memory issues

Status
Not open for further replies.

kittyyo

Programmer
Oct 17, 2001
89
AT
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.:

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
 
setting the length of a dyn array to 0 is not enough, assigning nil to it will actually release memory.
Code:
Setlength(Array,0);
Array:=nil;

By the way, why working with dynamic arrays if you have things like Tlist and TObjectList?

cheers

--------------------------------------
What You See Is What You Get
 
Thanks!

Think I'm either old-fashioned or insane ;-)

Do you know how much more memory would be used by TObjectLists (in comparison to dynamic arrays)... if I'd have 20000 of them? I think this was my first intention when I decided not to use lists... two years ago... ;-)

 
... and, BTW: Is there any tool that can point a programmer to code that assigns memory but doesn't free it?
 
look for memory sleuth for the memory leak hunt...

Objects are using a bit more memory in this case but even for 20000 items, this should be negligible nowadays...

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top