[fpc-pascal] understanding heaptrc and collection init()

waldo kitty wkitty42 at windstream.net
Mon Jan 23 19:24:25 CET 2012


i'm trying to understand what heaptrc is telling me... i have two collections 
that are set up, filled, used and disposed of... at the end of the program run, 
heaptrc pops up and tells me how much memory was allocated...

here's the definitions...

   Tcat_nbr   = pstring;
   Tsat_name  = pstring;
   Tline_data = pstring;
   PSCRec     = ^TSCRec;
   TSCRec     = object(TObject)
                  catnbr   : Tcat_nbr;
                  satname  : Tsat_name;
                  inorbit  : boolean;
                  constructor Init(cnbr,sname:string; iorbit:boolean);
                  destructor Done; virtual;
                end;
   PTLERec    = ^TTLERec;
   TTLERec    = object(TObject)
                  satname  : Tsat_name;
                  satdata1 : Tline_data;
                  satdata2 : Tline_data;
                  catnbr   : Tcat_nbr;
                  epoch    : double;
                  constructor Init(sname,sdata1,sdata2,cnbr:string; edate:double);
                  destructor Done; virtual;
                end;
   PSCColl    = ^TSCColl;
   TSCColl    = object(TSortedCollection)
                  Function Compare(Key1,Key2:Pointer):sw_integer; virtual;
                  Function KeyOf(Item:Pointer):Pointer; virtual;
                  Procedure Insert (Item:Pointer); virtual;
                end;
   PTLEColl   = ^TTLEColl;
   TTLEColl   = object(TSortedCollection)
                  Function Compare(Key1,Key2:Pointer):sw_integer; virtual;
                  Function KeyOf(Item:Pointer):Pointer; virtual;
                  Procedure Insert (Item:Pointer); virtual;
                end;


here's the initialization of the collections...

   aSatCatColl := New(PSCColl, Init(8192,64));
   aTLEColl := New(PTLEColl, Init(8192,64));

with these above init() numbers, heaptrc tells me the following...

Heap dump by heaptrc unit
708179 memory blocks allocated : 22513333/24646144
708179 memory blocks freed     : 22513333/24646144
0 unfreed memory blocks : 0
True heap size : 4521984 (96 used in System startup)
True free heap : 4521888

but if i reduce those init() values,

   aSatCatColl := New(PSCColl, Init(1,1));
   aTLEColl := New(PTLEColl, Init(1,1));

heaptrc tells me this...

Heap dump by heaptrc unit
735511 memory blocks allocated : 803685377/805873208
735511 memory blocks freed     : 803685377/805873208
0 unfreed memory blocks : 0
True heap size : 4521984 (96 used in System startup)
True free heap : 4521888

so what i'm not understanding is this... if i allocate one unit at a time for 
each item in the collection, why does it consume 80megs of space whereas 
allocating 8192 units to start and then 64 units at a time only uses ~22megs of 
space? and to continue with that, if i allocate 16384 to start with and then 64 
at a time, we're looking at this...

Heap dump by heaptrc unit
708008 memory blocks allocated : 14619829/16752640
708008 memory blocks freed     : 14619829/16752640
0 unfreed memory blocks : 0
True heap size : 4521984 (96 used in System startup)
True free heap : 4521888

which is down to ~14Meg of memory usage... in the above tests, the satcat 
collection loaded 16612 items and the TLE collection loaded 10897 items... 
everything was identical in all three tests...

i'm trying to keep my memory usage as low as possible without wasting too much 
by allocating for X number of possible items and only having Y items instead...




More information about the fpc-pascal mailing list