2005. május 7., szombat

How to reduce the window and GDI handles an application is using


I am trying to reduce the resources required to run my application. My application was pulling available resources down to 44%. I moved a large section of code containing two graphs out of an "available" form that does not get dynamically created and freed, into a separate form that is called with application.formcreate() and then freed after use.


Resources (the ones you can run out of in win9x) are things like window handles, menu handles, bitmap handles, handles of GDI objects like fonts, brushes, pens. These are not correlated with memory use or code size at all. All of these handles are used by Windows internally to reference some data structures for these objects (they are a kind of indirect pointer). The data structures are used by the 16 bit code that still makes up the core of Win9x/Me and come from a restricted pool of memory, a set of 64KByte memory blocks that are not extensible (the infamous USER and GDI heaps, USER and GDI are two of the core Windows modules).

The way to deal with resource-shortages is to reduce the number of window and GDI handles your app is using at any time. And the recipes for that are:

Do not autocreate your forms, with the exception of the main form. All other forms (at least the ones only used modally) should be created as needed and destroyed when no longer needed. Note that calling Close on a form will NOT destroy the forms memory image (and control handles), by default it only hides the form. For modeless forms you need a handler for the OnClose event of the form that sets the Action parameter to cafree. For modal forms you manually call the Free method of the form after the ShowModal call returned.
Try to replace controls that use Window handles with TGraphicControl descendents, e.g. TPanels and TGroupBoxes by TBevels. TGraphicControls do not use window handles, TWinControls do. Some, like TCombobox, even use more than one window handle.
If you use tabbed notebooks or pagecontrols a lot you can save resources by destroying the window handles of controls on hidden pages of the notebook, using the controls DestroyHandle method.
Replace groups of TEdit controls with a TStringGrid. A grid uses only two window handles, regardless how many cells it has.

Nincsenek megjegyzések:

Megjegyzés küldése