In this article:

General Information

Description

Example 1

Example 2

Example 3

Example 4

Examples of Code Snippets, After Executing Which Some Objects Remain Stored in Memory

Article number: KB000009

General Information

Related blocks:

Description

Application developers should pay special attention to using circular references in their code. Circular references need to be avoided in application code, that is, objects cannot refer to each other via system class objects. Garbage collector does not have information on the links to other objects (both system class objects and Fore objects) contained in system class objects, therefore 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:

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 structures similar to examples described below, and test memory state after executing your application using the Objects In Memory dialog box of development environment.

Example 1

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.

Example 2

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 the Cls1 class to ArList2, the memory will store only links to system classes but these classes cannot be seen. All Fore objects are released.

Example 3

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.

Example 4

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