"Mastering Delphi 6" learning notes seven

zhaozj2021-02-11  254

"Mastering Delphi 6" learning notes seven

ClassParent in TOBJECT is a very interesting method. With this method, you can start from a certain class, and you can describe a complete hierarchy list until the root TOBJECT of the entire VCL. You can check the inheritance relationship with the following example:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

VAR

Currobject: TOBJECT;

Currclass: Tclass;

Begin

Currobject: = sender;

While Currobject <> NIL DO BEGIN

Memo1.Lines.Add ('Class Name Is' Currobject.className);

Memo1.Lines.Add ('Instance Size IS' INTOSTOSTR (CURROBJECT.INSTANCESIZE);

Memo1.Lines.Add ('');

Currclass: = currobject.classparent;

IF currclass <> nil kil

Currobject: = currclass.newinstance

Else

Currobject: = NIL;

END;

END;

Note that the above code is actually problematic --- has used newInstance without release. However, as an example does not have to complicate, understand the usage.

The output of the above example:

Classname is TButton

Instance size is 536

Class name is TbuttonControl

Instance size is 524

Class Name is TwinControl

Instance size is 516

Class name is tcontrol

Instance size is 348

Class name is tcomponent

Instance size is 48

Class name is TPERSISTENT

Instance Size IS 4

Class name is tobject

Instance Size IS 4

It can be seen that both TOBJECT and TPERSISTEN are not included in any member variable (4 bytes for VTBL). It can also be seen that Tcontrol and TwinControl are quite a large class, which must have increased many member variables. And a TButton takes up to 536 bytes, that is, 2 buttons will exceed 1K, which is a bit surprising. (If you want to use sizeof, you won't get the right result: all the objects obtained with sizeof are 4, which means that the so-called object is actually a pointer.) It seems that the memory is getting bigger and bigger, but The components on a form should not be too much better.

Due to the accidental memory of TButton, I made a program, put it in the commonly used components, and see how the memory size is occupied. The code is simple:

Procedure TFORM1.FormCreate (Sender: TOBJECT);

VAR

i: integer;

Item: TLISTITEM;

Size: integer;

Begin

For i: = 0 to Componentcount-1 Do Begin

Item: = listview1.items.add;

Item.caption: = components [i] .classname;

Size: = components [i] .instancesize; item.subitems.add (inteltStr (size));

END;

END;

From the perspective, it can be led to the following conclusions:

1. Non-window components (such as Tshape, TBevel) are mostly more than 300 bytes;

2. Basic windowing components (such as TButton, Tedit, TListbox) of instancesize are mostly 500 to more than 600 bytes, and there are more occupied volumes that belong to Common Control;

3. Various mesh components (TDRID, TDRAWGRID) are large (more than 700 bytes), and TDBGRID even reaches 800 bytes.

4. All kinds of and database-related components are relatively large, generally in more than 700 to 900 bytes, only TDataSource is unexpectedly small-88 bytes.

5. Including the TDataSource mentioned above, there are several components that are also very small, they are TmainMenu, TactionList, Tcustomizedlg, TTIMER and TDATASOURCE, only 80-90 bytes, while other components have at least 300 bytes. . I didn't find which component's size was at 100-300 bytes.

6. In all components, INSTANCESIZE is TCHART, reaching 1024 bytes.

7. The new shellListView and ShellTreeView are also larger than the memory, and the number of bytes consumed by ShellTreeView reached 968 bytes, only in Tchart.

I don't know if you have seen this result, I think that Delphi is so slow to consume memory and start-up speed is also because of this reason. In the event of a good component, it is not abuse.

转载请注明原文地址:https://www.9cbs.com/read-3857.html

New Post(0)