Sunday, November 26, 2006
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:
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:
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.
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.
Comments:
<< Home
You are right that Delphi creates all forms by default like this. This makes things easier for beginner programmers, but most of the time you should disable this behaviour by (in BDS 2006):
- Tools | Options
- Environment Opiotions
- VCL Designer
- Uncheck "[ ] Auto create forms & data modules"
- Tools | Options
- Environment Opiotions
- VCL Designer
- Uncheck "[ ] Auto create forms & data modules"
Thanks for the hint!, i've found that Delphi 7 also includes this option in Tools->Environment Options->Designer->Auto create forms & data modules.
Post a Comment
<< Home