Article number: KB000009
Related blocks:
Application developers should pay special attention to using circular references in their code. Reference loops need to be avoided in application code, that is, objects cannot refer to each other via system class objects. Garbage collector does no have information on the references to other objects (both system class objects or Fore objects) contained in system class objects, thus these objects cannot be removed from memory after finishing the application.
When the components described in the form class are created dynamically, the user should take into account the following:
Parent component should be indicated for the component.
When event handlers are specified, additional references to these events are created.
If a component does not support defining parent component, but has any assigned event handlers, this component cannot be removed from computer memory after completing application work.
Avoid using in application code constructions similar to examples described below, and test memory state after executing your application using the Objects In Memory dialog box of development environment.
Public Class Cls1: Object
Public ArList: ArrayList;
End Class Cls1;
Public Class Cls2: Object
Public Field: Cls1;
End Class Cls2;
Sub Main;
Var
C1: Cls1;
C2: Cls2;
Begin
C1 := New Cls1.Create;
C2 := New Cls2.Create;
C2.Field := C1;
C1.ArList := New ArrayList.Create;
C1.ArList.Add(C2);
End Sub Main;
The Cls1, Cls2 and ArrayList objects remain stored in the memory after the Main procedure is executed.
Public Class Cls1: Object
End Class Cls1;
Sub Main;
Var ArList1: ArrayList;
ArList2: ArrayList;
C1: Cls1;
Begin
C1 := New Cls1.Create;
ArList1 := New ArrayList.Create;
ArList2 := New ArrayList.Create;
ArList2.Add(C1);
ArList1.Add(ArList2);
ArList2.Add(ArList1);
End Sub Main;
An instance of the class Cls1 remains stored in the memory after the Main procedure is executed. If you do not save an instance of Cls1 to ArList2 the memory will store only references to system classes, but these references cannot be seen. All Fore objects are freed.
Public Class Cls1: Object
End Class Cls1;
Class TestForm: Form
PopupMenu1: PopupMenu;
MenuItem1: MenuItem;
MenuItem2: MenuItem;
Sub TestFormOnShow(Sender: Object);
Var
C1: Cls1;
Begin
C1 := New Cls1.Create;
MenuItem2.Data := MenuItem1;
MenuItem1.Data := C1;
End Sub TestFormOnShow;
End Class TestForm;
An instance of Cls1 remains stored in the memory after the application has finished its work.
Class TestForm: Form
Button1: Button;
t: Timer;
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Begin
t := New Timer.Create;
t.OnTimer := OnTimer;
End Sub Button1OnClick;
Sub OnTimer(Sender: Object; Args: IEventArgs);
Begin
End Sub OnTimer;
End Class TestForm;
After the application stops running, the memory still stores an instance of the Timer component. This happens because parent component cannot be specified, when a component is created dynamically and the component is bound to event.
See also:
Developers Knowledge Base | Automatic Management of Object Life Cycle | Objects In Memory Dialog Box