[fpc-pascal] Question on how to avoid memory trouble using FindFirst(), FindNext() and FindClose()

Michael Van Canneyt michael at freepascal.org
Sat Feb 3 13:52:19 CET 2007



On Sat, 3 Feb 2007, Marco van de Voort wrote:

> > On Fri, 2 Feb 2007, Marco van de Voort wrote:
> > 
> > > > Can anyone recommend a method to search a whole drive, of arbitrary
> > > > size, without running out of memory.
> > > 
> > > I don't know seen SysTools, but I worked analysing logfiles for a year. All
> > > containertypes (TList TObjectList and TstringList included) that have a
> > > single array as internal datastructure become prone to fragmentation or
> > > slowdowns when the number of elements get bigger.
> > 
> > If you approximately know the number of elements in advance, you can
> > reduce the fragmentation to nearly zero if you set the capacity of the 
> > list before filling it.
> 
> The reallocations at the end hurt the most. So if you guess slightly too low it
> still fails.

Obviously, but it'll hurt less, since it grows with 25%; At most you'll get maybe 
1 reallocation, which is acceptable.

> 
> > It will also speed up the loading of the list.
> 
> (not if it is sorted).

Yes it will, because the reallocations don't happen as often. 
The sorting introduces an overhead anyway, whether you set capacity or not.

The correct procedure IMHO is
- Set capacity
- Load
- Sort

I tested such things with an N^3 algorithm for my daytime job, and the difference 
is very noticeable.

All this doesn't exclude that a specialized class may be more suitable for the job.
I just want to illustrate that, if programmed correctly, TList, TStringList and 
friends can still get you a long way...

Michael.



More information about the fpc-pascal mailing list