Garbage Collection

Welcome again readers!! Now our authors are back from the examination-season, and here Ms. Asha Bora presents an insight on Garbage collection in C# or rather in .NET Framework.

In C#, we can never destroy an object our selves. But, ever we thought why the C# designer’s did not give us this “RIGHT”? There are many good reasons for this .

Suppose we have given the responsibility to destroy an object then, there are chances that:
· We’d forget to destroy the object: then the object destructor would not run, leading to memory crises.
· We’d try to destroy an Active object, but what if suppose a class held a reference to this active object???.....then it would be a problematic situation.
· We’d try to destroy the same object more than once, but this can be disastrous, depending on the destructor’s code.
These problems are unacceptable in C#. Thus garbage collector is responsible for destroying the object.


Therefore, Garbage Collection is a process which automatically frees the memory of an object. It is a service which is provided by one of the most essential component of .NET Framework known as CLR(Common Language Runtime).
It occurs when an object is no longer needed, but it’s not necessary that it occurs immediately .Then runtime collects the garbage only when required i.e. when memory crises occur (low memory) and at that time it does as much as it can.

The garbage collection guarantees the followings:
· Each object will be destroyed and its destructor will run. When a program ends, all outstanding objects will be destroyed.
· Each object is destroyed only when it becomes unreachable i.e. when no reference refers to the object.
· Each object is destroyed only once.

Conclusion: thus we can say that garbage collection (GC) is tremendously useful and it frees the programmer from the tedious house keeping chores that are easy to get wrong and allow the programmer to concentrate on the logic of the program.

2 comments:

Unknown said...

good one but i guess its not of much use...this is something what every one knows...

anyways nice info for new user of C#.

Ritesh Raghuvanshi said...

Soniya thanks again...

We would like to inform you that now you can get the newest articles posted on our blog directly to your inbox, by registering your email ID in the subscribing box available on the blog.

Thanx and keep visiting...