Runtime will maintain all the information in the form of datastructure.Runtime will maintian a server called 'Garbage collector' to deallocate unused object memory, this is called automatic memory management.
- Mark and compact algorithm.
- Generation algorithm.
Mark and compact algorithm is providing logic for destroying objects. Generation algorithm is providing logic for optimization of garbage collector, this will improve performance.
When the object is created it will be placed into ganeration '0'. The objects bypassed for garbage collector call will be placed into generation '1'. The generation '1' objects will be moved to generation '2'. This makes scanning less number of objects for garbage collector. This will improve performance of garbage collector.
Runtime will call garbage collector when application requires memory. This is considered as an advantage because less amount of processor time will be given to garbage collector. This will not effect performance of application. Garbage collector is required for distributed application development, that is server based application. The developer can call garbage collector explicitly from application.
Destructor: Destructor is a subprogram of the class, which will be called implicitly before object destruction. The purpose is releasing resources consumed by objects. The resouece can be database connectivity, filestream,..so on. object class is a base class for all the classes under .net framework.
Releasing resources using destructor is not recommended for the following reasons.
- Resources will be engaged for more amount of time.
- It will be more burdens for garbage collector.
The solution is providing a custom method called dispose along with finalize method. The finalize method should be considered when the dispose method is not called. When dispose method is called, provides instructions to garbage collector to avoid execution of finalize method. These instructions can be provided using suppress finalize method.
An example application is given below, please download it to know how garbage collector works.
Comments/Suggestions are invited. Happy coding......!