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,
- Destructor
- Dispose()
- Finalize
Destructor
- A destructor is a class member used to destruct the instances, deallocating the memory, releasing of resources etc.
- The destructor is called/invoked automatically by .NET Garbage Collector (GC). We can not manually invoke or control it.
- Destructor cleans the memory used by the instances.
- 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.
- Destructor name is same as the class name followed by '~' symbol.
- The Syntax of Destructor is,
Eg.
- class Demo {
- ~Demo() {
-
- }
- }
- '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"
- class Demo
- {
-
-
- static~Demo() {
-
- }
- }
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".
- class Demo {
-
-
- public~Demo() {
-
- }
- }
- 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".
- class Demo {
-
-
- int~Demo() {
-
- }
- }
and similarly, we have declared destructor with parameters then the compiler will generate an error,
- class Demo {
-
-
- ~Demo(int x, string str) {
-
- }
- }
- Destructor can only be used with classes, not with struct.
- struct Student {
-
- string Name;
-
-
- ~Student() {}
- }
when we define destructor in struct then compiler will generate an error as "Only class types can contain destructors"
- A destructor can neither be inherited nor be overloaded.
- Unlike Constructor, the destructor of child class called before parent class.
- using System;
- using System.Collections.Generic;
- namespace Tutpoint {
- class Program {
- static void Main(string[] args) {
- child child = new child();
- Console.ReadLine();
- }
- }
- class Parent {
-
- ~Parent() {
- Console.WriteLine("Destructor of Parent Class");
- }
- }
- class child: Parent {
-
- ~child() {
- Console.WriteLine("Destructor of child Class");
- }
- }
- }
output
Destructor of child Class
Destructor of Parent Class
- 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.