Collection Process: Collection process is nothing but destroying the unused objects & collecting the memory from destroyed objects.
To perform the collection process garbage collector is depending on generation algorithm
According to generation algorithm complete managed heap will be dividing into three generation like below.
Generation '0': In this generation heap will have just now created objects.
Generation '1': In this generation heap will have just before created objects.
Generation '2': In this generation heap will have long before created objects.
As per above diagram managed heap divided into 3 generations.
Generation '0' is having 2 objects objE and objF. In this "objE" objects is recognized as unused object.
In generation '1' we have 2 objects they are objC and objD. In this objC is recognized as unused object.
In generation '2' we have 2 objects they are objA and objB. In this objA is recognized as unused object.
To perform the collection process garbage collector will visit the managed heap periodically.
According to generation algorithm garbage collector will visit the generation '0' & it will destroy the recognized unused objects within generation '0'.
According to above diagram in generation '0' objE is unused object garbage collector collector will destroy objE object & the destroyed object released memory it will provide to newly created object.
If the generation '0' destroyed object released memory is not sufficient for the newly created object then garbage collector will visit generation to perform collection process.
According to above diagram generation '1' is having one unused object i.e., objC it will destroy objC object & objC object released memory is also not sufficient for newly created objects then garbage collector will visit the next generation called generation '2' to perform collection process.
According to above diagram generation '2' is having one unused object i.e., objA garbage collector will destroy objA object if that destroyed object released memory is also not sufficient for newly created object. Again garbage collector will visit the generation '0', generation '1', generation '2' to perform collection process. It is a cycling process.
Garbage collector will get much memory from the generation 2 and generation 1 only. It will get very less memory from generation 0 because generation 2 and genration1 will have much unused object & generation 0 will have very less number of unused object because which are all guest now created object.
In this way garbage collector will perform the memory management in .NET
An object may destroy in 2 ways: 1. By garbage collector 2. By programmer When an object is destroyed by the garbage collector concern destructor will execute as part of the distructor execution finalize method execute that means finalize method is calling by the garbage collector.
Dispose(): Whenever a programmer is recognized that particular object is not going to used in future applications then he will destroy that obj by using disposed method that means dispose method is invoking by the programmer.