Thoughts and attempts on saving memory and accelerating startup when using multiple forms

Source: Internet
Author: User
When I first came into contact with Delphi, I thought PI was a constant. when I knew it was a function, I was touched.

However, PI only returns the extended type that occupies 10 bytes. Later, we found that some objects are doing the same thing, for example:

 
Function clipboard: tclipboard; {clipbrd} function printer: tprinter; {printers} function versions: tlanguages; {sysutils} function libraries: libraries; {treeintf} function comclassmanager: tcomclassmanager; {comobj} function themeservices: tthemeservices; {themes} function propertycategorylist: tpropertycategorylist; {propertycategories} // This may be all of the similar objects in Delphi 2010.

  

In particular, commonly used clipboard, printer, and ages can be used like objects without having to create them.

How is this implemented? View the source code. They all have a similar structure (take the clipboard object as an example ):

Interface... function clipboard: tclipboard; {This is generally at the end of the interface }... implementation... vaR fclipboard: tclipboard; {handle variable }... function clipboard: tclipboard; {function Implementation} begin if fclipboard = nil then fclipboard: = tclipboard. create; Result: = fclipboard; end ;...

  

Of course, they still have a release issue, but I think these objects are often used frequently as the units where they are located. Generally, they are released at the end.

They are clever:
They are not created when we do not use them;
Create immediately when used;
It will not be created again when used again.

It's a very simple and clever idea!

You must not mention two other similar objects:

 
Application {forms. tapplication} screen {forms. tscreen} // these two are more common, but they are not functions, they are indeed object variables; they areProgramThe lifetime has always existed.

  

When a project has multiple forms (I'm afraid there are few programs for a single form), can those forms be dynamically created? This will definitely save resources and speed up startup. I don't need to test it.

The above practices give us a good reference, but I have come up with two methods: 1. dynamic creation; 2. Using functions as above.

No matter which method is used, you must first disable automatic creation of the form by default by the program. Modify:

Of course, you can also directly Delete the relevant establishment from the project file.Code.

 
// At this time, we can no longer use the following code to open the form: Procedure tform1.button1click (Sender: tobject); begin form2.show; {this will cause an error because form2 has not yet established} end; // you can do this: Procedure tform1.button1click (Sender: tobject); begin form2: = tform2.create (application); form2.show; end; // if you want to do this, after form2, the memory will reside again. How can we release it? Do you want to release it together with the application? // Our goal is to save memory. Can this happen? : Procedure tform1.button1click (Sender: tobject); begin form2: = tform2.create (application); form2.show; {But form2.showmodal; can} form2.free; end; // try the above Code, the form is released just after it is displayed! // There is a solution. In the onclose event of form2, write the following code: Procedure tform2.formclose (Sender: tobject; var action: tcloseaction); begin action: = cafree; {canone, cahide, cafree, and caminimize are: Cancel, hide, release, and minimize} end; // The Call code in form1 can also be simplified as follows: Procedure tform1.button1click (Sender: tobject ); begin with tform2.create (NiL) do show; end;

  

This is the first solution. The second solution is to use clipboard and other design techniques.

Imitating the implementation of clipboard, we can rewrite the unit2 unit where form2 is located:

 
Unit unit2; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs; Type tform2 = Class (tform) Procedure formclose (Sender: tobject; var action: tcloseaction); end; function form2: tform2; implementation {$ R *. DFM} var fform: tform2; function form2: tform2; begin if fform = nil then fform: = tform2.create (application); Result: = fform; end; Procedure tform2.formclose (Sender: tobject; vaR action: tcloseaction); begin action: = cafree; end.

  

There is a difference between the form and clipboard, because the code can be simplified:

Unit unit2; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs; Type tform2 = Class (tform) Procedure formclose (Sender: tobject; var action: tcloseaction); end; function form2: tform2; implementation {$ R *. DFM} function form2: tform2; begin result: = tform2.create (NiL); end; Procedure tform2.formclose (Sender: tobject; var action: tcloseaction); begin action: = cafree; end; end.

  

In this way, we can call the following in the main form:

 
Procedure tform1.button1click (Sender: tobject); begin form2.show; end; {the default difference between this and Delphi is big. Here form2 is a function; dynamic creation and Dynamic Release}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.