Hi Friends,
Here I gave detailed explanation of garbage collection.
Below are the questions that would you get about garbage collection when you want to know about it.
1. What is garbage collection?
2. Who is responsible for doing this?
3. How does garbage collection happen?
What is garbage collection?
Garbage collection is the cleaning up of objects in the managed head which are no longer needed.
Who is responsible for doing garbage collection?
Garbage collection is the responsibility of the CLR.CLR does this on its own in regularly. Developer doesn’t need to write anything for this. However, there are some scenarios developer has to write code for cleaning up of the objects explicitly which will be explained in the later section of the post.
How does garbage collection happen?
I already mentioned before CLR is responsible for cleaning up of the unused objects regularly. Now you get doubts like How frequent Garbage collector runs and how does it determine objects to be cleaned up.
Before going to answer above questions let me explain Generations concept.
CLR which is responsible for running of garbage collector should check each and every object that has been created in the managed heap to check whether it’s still needed.
But if we observe properly there will be some objects which should be in the memory for long time for example in Windows Application, Main window object of the application will be needed in the memory until application is closed unlike objects created local to methods, so its waste of time for CLR to visit every object and later realizing that It can’t free them.
Hence CLR classifies objects into different generations like Generation 0,Generation 1 and Generation 2.
CLR visits Generation 0 objects more frequently than other 2.
Based on the object requirements objects are assigned to specific generations.
Generation 0: A newly created objected which has never been visited by garbage collector.
Generation 1: An objected which has survived garbage collection
Generation 2: Identifies an object that has survived more than one sweep of the garbage collector.
The garbage collector will investigate all the generation 0 objects first. If it can get the required amount of memory any surviving objects are marked as generation 1.
If even after cleaning up some generation 0 objects if still its need of some more space, garbage collector will go and check in generation 1.If still needed generation 2 objects are evaluated.
So it is clear that by assigning a generation value on the heap, most recently created, no longer needed objects are cleaned up first and older objects are not visited much for clean up.
Forcing Garbage Collection:
Garbage collector claims the memory on behalf of us.In some scenarios we have clean up memory explicitly.
1. You don’t want your application to be interrupted.
2. If you have created huge number of objects and you want clean up any space.
Cleaning up unmanaged objects:
Finalize() method is for cleaning unmanaged objects.
There could be cases where you might be calling unmanaged methods inside your C# code.
Any garbage collector before cleaning up managed resources creates object graph for unmanaged objects.
All the objects that have to run Finalizer are Finalizable Objects.
Destructors internally implement Finalize()
Cleaning up unmanaged memory with Dispose() method:
As we have already seen Finalizers are used to clean up the unmanaged objects when garbage collector is collected.
In case if you have large memory space that is being used by unmanaged resource and has to be cleaned you may not want to wait for Garbage collector.In this case we gave Dispose()\
In order to implement Dispose() need to implement IDisposable interface.
Building Finalize –Dispose Pattern:
In the above method we used Dispose(),If we forget to call Dispose() Unmanaged memory will never be cleaned up.
So we use Finalize Finalize Dispose pattern.In this we forget to call Dispose(),Finalize will be called.
Example code :
Please don't forget to share your feedback.