Wednesday, March 18, 2009

Is Delphi a memory predator?

In this article, i want to show why the Delphi way of development of visual applications is not as cleaner as you should want. When I say clean, i mean in memory terms, it mantains an instance of every form in memory, even if you don't want it.

To show it in an example, just create a new application and add two forms. The code in your Project1.dpr file is similar to this:

program Project1;

uses
Forms,
form1 in 'form1.pas',
form2 in 'form2.pas';

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end.


The Application.CreateForm method creates an instance of a form and mantains it in memory until Application object is destroyed. That means the form is in memory even if the program doesn't requires it.
I remember some candidates for the Coding Horror blog, applications with tens of forms created in this way. That's not a programmer's fault, it's the fault of the Delphi IDE, and its Help, and many books that teach how to create elegant and scalable Delphi applications.

Keeping control of the forms

Every time you create a form in Delphi, it adds its unit to the uses of the project (.dpr file) and an Application.Create(...) between Application.Initialize and Application.Run methods. To keep memory usage low, you must delete those instructions from the project file, and also the variable (like var Form1: TForm1;) declared just below "implementation" declaration in the form's unit.

If you want to use the form, for example when an event of the main form is called, just create it and free inmediately after you use it:

procedure TForm1.Button1Click(Sender: TObject);
var
Form2: TForm2;
begin
Form2 := TForm2.Create(nil);
try
Form2.ShowModal;
finally
Form2.Free;
end;
end;


Just be careful to include form2 unit in the uses clause of form1 and remove the Form2 variable that Delphi automatically creates when you design the form.
The example above creates a "modal" form, if you want to create a "modeless" form, you must create it using the "standard" Delphi way.

No comments: