Destructor. “Garbage collection”

Destructor. “Garbage collection”


Contents


1. What is the “garbage collection” in C#?

When program is running, when class objects are created, there is an allocation of memory by the new operator. After when the object did the work, it (may be) isn’t used in the program (but memory is allocated for it). If a program created many classes of objects, a situation may arise when the allocated memory is not enough to create the next object.

In this case, is important free the allocated memory, that were allocated for the objects, which are not used already. For this the system of dynamical memory allocation C# uses so-called “garbage collection”. If there is a “garbage collection” then frees the memory for objects that are not used.

 

2. How the system of “garbage collection” defines: is used the object or not?

To define, is used object or not, the system of “garbage collection” scans the references to this object. If the references are not available, then this object is not used and it is needed free the memory for it. In the following this frees memory can be used for reallocation of others objects. Further this freed memory can be used for the distribution of other objects.

 

3. How often occurs the “garbage collection” in the system of dynamic control of memory C#?

If the object that is used becomes unnecessary, the memory that allocated to it, cannot be free immediately, but it can be free later. In the system of dynamical memory control C#, “Garbage collection” occurs only from time to time when program is running. The “garbage collection” cannot occurs, if there are few objects that are not used.

 

4. What is the purpose of using destructors?

There are cases in the program, when it is needed execute the actions before the freeing of the memory, that were allocated to the object. Destructor is the method which is called for guarantee freeing memory for the object.

In the destructor are pointed the actions, which is needed to do before the object will deleted.

The general form of destructor is following:

~class_name()
{
   // the code of destructor
}

where class_name – name of the class.

Destructor has no returned type and has no parameters list.

 

5. When the destructor is called? Example of destructor in class

In C#, it is not possible to explicitly call the destructor, because there is no special operator for this. For example, in the C++ language, the operator delete is used for these purposes, which is not used in the C# language. But in C++, the garbage collection system is not implemented.

However, the destructor is automatically called by the “garbage collection” system before destroying memory for an object of its class.

Example. The example declares the class MyClass, in which the ~MyClass() destructor is implemented.

// example of a class in which the destructor is implemented
class MyClass
{
    int[] A; // internal array A

    // class constructor
    public MyClass()
    {
        // memory allocation for array A
        A = new int[100];
    }

    // destructor
    ~MyClass()
    {
        // actions that must be performed if “garbage collection” occurs,
        // and destructor can be called
        // ...
    }
}


Related topics