Garbage
Collector is available in CLR of .net framework and it is responsible for complete memory management for the .net applications.
Allocating memory for the objects and de allocation of memory when their life
time is completed is the responsibility of garbage collector.
Various steps performed
by garbage collector when it was invoked are as follows.
- Suspends all the threads in .net application and it will run on a
separate thread.
- It draws a graph of all reachable objects. An object that contains
a reference to heap memory area is called as reachable object and the
object that contains null is called as unreachable object. All the objects
that are not in the graph will be unreachable objects.
- All unreachable objects with a destructor will be added to a
queue called Freachable queue.
- All unreachable objects without a destructor are destroyed and all
reachable objects will be moved down the heap to make free memory
available at the top of the heap. While moving reachable objects down the
heap, garbage collector will update the objects to refer to new memory
location.
- Resumes the threads of the .net application.
- Destroys all unreachable objects with a destructor available in freachable queue.
Note
Garbage collector will be invoked only in the
following two situations.
1.When there is no sufficient memory for new
objects.
2.When the application execution comes to an
end.
If you want to invoke the garbage collector
manual then call the collect() method of GC class available in .net framework.