Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
Previous Newsletters

Products Articles Downloads Order Support
Customer Portal      

Delphi 3,4,5 TCollection Performance Issues and Solutions

Submitted on June 2, 2002

If you are using TCollection classes in your Delphi 3,4 or 5 applications than you will find this article quite interesting.

Firstly let us try fairly simple code:

procedure TForm1.Button1Click(Sender: TObject);
   old: TCollection;
   i: integer;
   old := TCollection.Create(TCollectionItem);
   for i := 0 to 100000 do
   Windows.beep(900, 1000); // hi-freq beep after we done with adding empty items
   Windows.beep(100, 1000);// low-freq beep after we done with destroying empty items

You might think that low-freq beep will follow right after hi-freq beep (well what can be faster that just simply destroy all collection items) - but IT IS not!

In fact it takes 10-20 seconds to destroy collection which hold few dozen thousands items - and worse of all your CPU will be 100% busy. We bumped into this problem when our clients complains that application is "freeze PC for a few minutes".

To understand why it is happening you need take closer look at TCollectionItem.Destroy, TCollectionItem.SetCollection and TCollection.RemoveItem functions which located at classes.pas - last one is the key to understanding this problem.

You also might want to compare your TCollection.RemoveItem version to Delphi 6 TCollection.RemoveItem code:

{ classes.pas from Delphi 6 }
procedure TCollection.RemoveItem(Item: TCollectionItem);
   Notify(Item, cnExtracting);
   if Item = FItems.Last then
      FItems.Delete(FItems.Count - 1) // that will fix original problem
   Item.FCollection := nil;
   NotifyDesigner(Self, Item, opRemove);

Now you probably will want to fix it. But seems it is not so easy because TCollection.RemoveItem is not a virtual or dynamic function.

Here is two solutions:

  • You will need to alter classes.pas - put that TCollection.RemoveItem code from Delphi 6 into your version of classes.pas.

Copy new (fixed) classes.pas into your project directory and put it at the first position in your .dpr uses section like this:

program Project1;

   classes in 'classes.pas' // new classes.pas with fixed TCollection.RemoveItem
   Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

   Application.CreateForm(TForm1, Form1);

Now your project will be compiled with new version of TCollection.

  • In some situations it is not so convenient or even not possible to use altered classes.pas and in this case we have other trick for you.

{ TFixCollection - fix TCollection.RemoveItem issue in Delphi 3,4,5 }
   TFixCollection = class(TCollection)
   { Unfortunately Clear is not a virtual or dynamic procedure so we will have to reintroduce it }
      procedure Clear;
      destructor Destroy; override;

procedure TFixCollection.Clear;
   i: integer;
   AList, OrgList: TList;
   AList := TList.Create;
      OrgList := TList(PDWORD(DWORD(Self) + $4 + SizeOf(TPersistent))^);

      { Save original pointers to collection items }
      for i := 0 to OrgList.Count-1 do


      { Destroy collection items }
      for i := 0 to AList.Count-1 do

destructor TFixCollection.Destroy;

{ Let's try again ! }
procedure TForm1.Button2Click(Sender: TObject);
   old: TFixCollection;
   i: integer;
   old := TFixCollection.Create(TCollectionItem);
   for i := 0 to 100000 do
   Windows.beep(900, 1000); // hi-freq beep after we done with adding empty items
   Windows.beep(100, 1000);// low-freq beep after we done with destroying empty items

As you can see now it works just fine. We used one trick which gives us access to protected section of TCollection.

You can use both techniques in your applications written on Delphi versions 3,4,5.

For your convenience you can download Delphi 3,4,5 TCollection performance issue demo sources here

Clever Components Support Team.
Please feel free to Contact Us
We are always glad to help you.

    Copyright © 2000-2022