Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Destructors in C# (OOP’s) – Part 3

Posted by Ravi Varma Thumati on July 13, 2009

Every object created uses various system resources like the memory which are reserved for the object’s use until they are explicitly released. If all references to object that manage the resources are lost before the resource is explicitly released, the application can no longer access the resources to release it and this called {memory leak}.

Dot Net Framework Common Library [FCL], had produced a way to give back the resources to the system when they are no longer needed. Common Language Runtime [CLR] performs automatic memory management by using the “Garbage Collector”, to reclaim the memory occupied by the objects that are no longer in use so, memory can be used for other objects.

When there are no more references to an object, it becomes eligible for destruction. Every object has a special member called “Destructor”, which is invoked by the garbage collector to perform termination housekeeping on an object, just before the garbage collector reclaims the object’s memory. After the garbage collector calls the object’s destructor, the object becomes eligible for garbage collector. The memory for such an object can be reclaimed by the garbage collector.

What is Destructor?

Is a special method like any method, and we can able to ignore it and didn’t write it in the class, because the CLR will be automatically did a management for the memory, and reclaim the unreferenced objects.

The destructor has the following properties, which distinguish it from other methods:

  • Has the same name as the class.
  • Doesn’t return any value.
  • Called automatically when the object is destroyed.
  • Can not be defined in the struct.
  • The class can only have one destructor.
  • It can not be inherited or overloaded.
  • It can not be called, because it is invoked by the garbage collector.
  • It is called when the program exits.
  • It doesn’t take any access modifier.
  • It doesn’t take any parameters.
  • It is denoted by the sign (~) and pronounces “tiled”.

Simple Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Destructor

{

//initiate a class

class SampleClass

{

//define the fields

private int x = 4;

//destructor

~SampleClass()

{

Console.WriteLine(“\n\tthe value of x is: {0}”,x);

}

}//end MyBook class

//initiate another class that inherit from the first class

class Program

{

static void Main()

{

//creating object from the class

SampleClass mysampleclass = new SampleClass();

//using the garbage collector to invoke the destructor

GC.Collect();

//print data

Console.WriteLine(“\n\n”);

}

}

}

The Output of this example will be as the following:

After printing the value of the instance variable x, it will destroy its value from the memory.

Destructor Working:

The destructor implicitly calls the “finalize” method on the objects base class, so the compiler understanding the code of the destructor as the following:

protected override void Finalize()

{

try //try block for first testing

{

//statements that you need to execute

}

finally //finally always execute its code in any case

{

base.Finalize();

}

}

The finalize method allows an object to attempt to free resources and perform other clean up operations before the object is reclaimed by the garbage collector.

Garbage Collector:

Checks for objects that are no longer being used by the application

Steps of Working:

The garbage collector working according to the following three steps:

  • Searches for managed objects that are referenced in the managed code.
  • Attempts to finalize objects that are not referenced.
  • Frees objects that are no referenced and reclaim the memory.

Declaring:

We can oblige the garbage collector to work by using the build-in static class that called “GC” , and the method “Collect” as the following:

//using the garbage collector to invoke the destructor

//oblige the garbage collector to work

This way oblige the garbage collector of all generation, and it is the widely use. We also can oblige the garbage collector to work with the same class, but with using of another method which oblige it for all generation from [0] through specific generation. To have the garbage collector reclaim objects up to a specified generation of objects, use the GC.Collect(Int32) method overload. When you specify the maximum generation, all objects are collected.

//using the garbage collector to invoke the destructor

//oblige the garbage collector to work

Using GCCollectionMode.Optimized, to determine that, is this an optimized time to reclaim the memory or not.

Example:

class Program

{

static void Main()

{

//creating object from the class

//and the object will reserve a location in the memory

SampleClass mysampleclass = new SampleClass();

//when making the object is null

//it will not be in use

//but it still has the location in the memory

mysampleclass = null;

//oblige the garbage collector

//to invoke the object destructor

//to reclaim the memory

GC.Collect();

}

}

If the destructor writes to console any data, we must use the following method with the garbage collector

GC.WaitForPendingFinalizers();

This method will make the garbage collector wait until the destructor finish its work completely, and then reclaim the memory.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: