Friday, June 27, 2008

What is garbage collection?

Garbage collection is a CLR feature which automatically manages memory.
Programmers usually forget to release the objects while coding ..... Laziness (Remember in VB6 where one of the good practices is to set object to nothing).
CLR automatically releases objects when they are no longer inuse and refernced.
CLR runs on non-deterministic to see the unused objects and cleans them. One side effect of this non-deterministic feature is that we cannot assume an object is destroyed when it goes out of the scope of a function. we should avoid using destructors because before GCdestroys the object it first executes destructor in that case it will have to wait for code to release the umanaged resource. resultin in additional delays in GC.
So its recommended to implement IDisposable interface and write cleanup code in Dispose method and call GC.SuppressFinalizemethod so instructing GC not to call your constructor.