We generally have this question in most of the interviews on Garbage collector
1) What is the need of Garbage Collector?
2) How does the Object Generations come into the picture?
3) How the objects get Disposed based on Generations?
4) Will an Object will be moved from One Generation to another?
5) Why there are multiple generations when we are anyways clearing the memory?

 Posted by Chvrsri on 3/16/2016 | Category: ASP.NET Interview questions | Views: 2237 | Points: 40
Answer:

Garbage Collection is the most important topic which we hardly notice the use of it, Courtesy .Net CLR made it to run automatically without invoking it. Which made us lazy to understand what that is exactly used for.

Question 1: What is the need of Garbage Collector?
Answer 1: So when we ever we create any object using New operator the memory of that respective object gets stored in the Heap memory. So our application size increases when we tend to create more and more objects and sooner or later the Heap memory gets filled completely without even having space to save any object. Now in this case the Garbage Collector gets called automatically and checks for unused objects in that heap memory. So it cleans up those unused objects and allocates space for newly created Objects into Heap memory

Question 2: How does the Object Generations come into the picture?
Answer 2: The role of object generations comes into picture when CLR checks for objects which are no longer used in the application since a very long time and then Garbage collector will reclaim its memory.

Question 3: How the objects get Disposed based on Generations?
Answer 3: In general we have 3 generations for an object to travel which was written under the .Net framework library. By default the object goes into Generation 0.

Question 4: Will an Object will be moved from One Generation to another?
Answer 4: So when the Generation 0 objects gets completely occupied with memory now the Garbage collector will check for the objects which are unused for long time and will mark them for deletion and will move the remaining objects to Generation 1. Same is the behavior with Generation 2.

Question 5: Why there are multiple generations when we are anyways clearing the memory?
Answer 5: Because of having multiple generations the Garbage Collector need not check the entire Heap memory pool rather than it first checks for Generation 0 and will reclaim the memory. If it still needs it will check in Gen 1 and Gen 2. Which will decrease the time in finding the object and consequently enhancing the application performance.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Amatya on: 3/17/2016 | Points: 10
Nice 1.. question 5 was new to me

Thanks for sharing

Login to post response