Garbage Collector And Destructor In C#

In this article, I am going to talk about Garbage Collector and Destructor in C#.

What is Garbage Collector?
  • Garbage collector looks for an instance/object which is not required anymore and treats these objects/instances as ready to destroy. Then, it makes a call to the destructor to release the memory and deallocate the resources.
  • Garbage collector ensures that all instances that are no longer used by the application should be destroyed.
  • Garbage collector keeps the track of all the instances.
  • Garbage Collector cleans the memory by three ways,
    1. Destructor
    2. Dispose()
    3. Finalize
Destructor
  1. A destructor is a class member used to destruct the instances, deallocating the memory, releasing of resources etc.
  2. The destructor is called/invoked automatically by .NET Garbage Collector (GC). We can not manually invoke or control it.
  3. Destructor cleans the memory used by the instances.
  4. An instance will automatically be destroyed or destructed when an instance become useless or the process of that instance is completed. So the execution of destructor may take place any time when an instance become useless or ready for destruction.
  5. Destructor name is same as the class name followed by '~' symbol.
  6. The Syntax of Destructor is,
    1. ~<Class_Name>()  
    2. {  
    3.    // Implementation  
    4. }  
    Eg.
    1. class Demo {  
    2.     ~Demo() {  
    3.         // Implementation of Destructor  
    4.     }  
    5. }  
  7. 'static' keyword and access modifier are not allowed on destructor. In this program, we have declared destructor with 'static' keyword and compiler will generate an error as "The modifier 'static' is not valid for this item"
    1. class Demo   
    2. {  
    3.     // Destructor cannot declare with 'static' keyword  
    4.     // On implementing, compiler will generate an error as "The modifier 'static' is not valid for this item"  
    5.     static~Demo() {  
    6.         // Implementation of Destructor  
    7.     }  
    8. }  
    and similarly, we have declared destructor with 'public' access modifier then the compiler will generate an error as "The modifier 'public' is not valid for this item".
    1. class Demo {  
    2.     // Destructor cannot declare with access modifiers  
    3.     // On implementing, compiler will generate an error as "The modifier 'public' is not valid for this item"  
    4.     public~Demo() {  
    5.         // Implementatimplementationctor  
    6.     }  
    7. }  
  8. Parameters and return type are also not allowed on destructor.

    In this program, we have declared destructor with 'return type' keyword, then the compiler will generate an error as "Invalid token '~' in class, struct, or interface member implementaion".
    1. class Demo {  
    2.     // Destructor must not contain return type  
    3.     // On implementing, compiler will generate an error as "Invalid token '~' in class, struct, or interface member implementaion"  
    4.     int~Demo() {  
    5.         // Implementation of Destructor  
    6.     }  
    7. }  
    and similarly, we have declared destructor with parameters then the compiler will generate an error,
    1. class Demo {  
    2.     // Destructor must not contain parameter  
    3.     // On passing parameters, compiler will generate an error  
    4.     ~Demo(int x, string str) {  
    5.         // Implementation of Destructor  
    6.     }  
    7. }  
  9. Destructor can only be used with classes, not with struct.
    1. struct Student {  
    2.     //Variables  
    3.     string Name;  
    4.     // Struct can not contain destructor  
    5.     // Compiler will generate an error as "Only class types can contain destructors"  
    6.     ~Student() {}  
    when we define destructor in struct then compiler will generate an error as "Only class types can contain destructors"

  10. A destructor can neither be inherited nor be overloaded.

  11. Unlike Constructor, the destructor of child class called before parent class.
    1. using System;  
    2. using System.Collections.Generic;  
    3. namespace Tutpoint {  
    4.     class Program {  
    5.         static void Main(string[] args) {  
    6.             child child = new child();  
    7.             Console.ReadLine();  
    8.         }  
    9.     }  
    10.     class Parent {  
    11.         // destructor  
    12.         ~Parent() {  
    13.             Console.WriteLine("Destructor of Parent Class");  
    14.         }  
    15.     }  
    16.     class child: Parent {  
    17.         // destructor  
    18.         ~child() {  
    19.             Console.WriteLine("Destructor of child Class");  
    20.         }  
    21.     }  
    22. }  
    output

    Destructor of child Class
    Destructor of Parent Class

  12. Destructor without any statement or blank destructor should not be used. Else it may result in the loss of performance.
Dispose()

The Dispose() method can be called directly just like any other method. Dispose() method contains code to dispose of the managed or unmanaged resources accessed by the instance or object.

Finalize

Finalize method can be directly called using <objectname>.Finalize(); syntax. Finalize method clean the memory used by the class.

Conclusion

Garbage Collector (GC) frees up the memory by three ways: destructor, dispose(), finalize. A destructor cannot be called directly but dispose() and finalize can be called directly. A destructor can only be called by GC. I hope this article helps you to understand a bit more about Garbage Collector and Destructor.

Thank you. Please feel free to ask any question or make a suggestion.


Similar Articles