.NET C# Interview questions and answers :- What is Garbage Collector, Gen 0, 1 & 2?

.NET C# Interview questions  and answers :- What is Garbage Collector, Gen 0, 1 & 2?


Lets start with Garbage collector video series In this video series we have two videos The first video which talks about what is garbage collector What is the concept of generation i.e generation 0, generation 1 and generation 2 In the second video we will try to understand by using distructors in the class we end up into double garbage collector log Try to understand the basics fundamental, why do we need garbage collector, what is the use of garbage collector in .NET Garbage collector is feature provided by CLR which helps us to clean unused manage objects By cleaning those unused manage objects It reclaims the memory When your .NET application runs it can create several objects and at a given moment of time its very much possible it will not use some of those objects or it will not need some of those obejcts For those objects the garbage collector runs as a back ground thread continuously At specific interval it checks if there are any kind of unused manage objects If it finds any it just goes and reclaims the memory Garbage collector is background thread which runs continuously and which checks if there are any unused manage objects and basically cleans those objects and reclaims a memory Garbage collector only cleans unused managed objects It doesn’t clean unmanaged objects. In other words anything which is outside the CLR boundary the garbage collector doesn’t know how to clean it Attach to garbage collector and there is one more important concept called as generations There are three kinds of generation, generation 0, generation 1 and generation 2 Try to understand the concept of generation and how does it affect garbage collector performance Try to understand the concept of generation 0 1 and 2 You have a simple application called as App1 This is a windows application It recently started As soon as the application started It created five objects As soon as application creates new objects or fresh object These objects are moved into a bucket called as generation 0 These five objects which are freshly created are first move to the bucket or its categorized into a bucket called as generation 0 All of these objects are managed object If you have any kind of unmanaged objects like Excel component or VC++ or com component The garbage collector fundamental soon apply for them Five manage objects are created and as soon as they created they are pushed into bucket called as generation 0 Garbage collector which is a background thread and nobody knows when he will come and visit the application to see which of the objects are unused The garbage collector comes once in a blue moon and says from these five objects which are the objects which are not needed currently buy the application Two objects are no needed by the application It removes or reclaimns the memory of these two objects The remaining three objects are yet needed. In other words the application needs them For the garbage collector he cannot claims these memory All these three objects are moved to the generation1 bucket They are pushed ahead to the generation1 bucket It creates more fresh objects. It creates more two fresh objects The garbage collector again comes after some un datamistic period Let me see which objects are now not needed It goes and checks for the generation 0 bucket This generation 0 bucket, both of these objects can be claimed. It claims and cleans them It goes to the generation 1 bucket and says you three guys I had not claimed your memory Are you still needed in the application? One of the person says yes he is needed And the other one says they are not needed The remaining two goes and reclaims their memory The last one which still needed by the application, he moves that object to generation 2 He removes them from the generation 1 and he moves them to generation 2 In other words generations define how long the object is staying in the memory or how long the object has been in the memory What is the concept of this generations When we talk about big applications they can create 1000 of objects or 100 of objects For each of this objects if your garbage collector has to go and see them if they are needed or not its a huge process or bulky process By creating these buckets if a object is in generation 2 he will make list amounts of visits over here When the object has move to generation 2 that means he will stay more time then memory. There is no point going and asking them should I reclaim your memory or not In the same way generation 1 objects are more likely to win the memory then generation 0 Is there are 1000 of objects and 200 objects are in generation 1 and generation 2 Then he will only browse the 800 objects In other words generation 0, generation 1 and generation 2 help to increase a performance of garbage collector We had seen the concept of garbage collector and generation 0, generation 1 and generation 2 Try to visually see how these generation looks like or even is it possible to see the inner memory of the garbage collector In order to see the managed memory in .NET there is a tool called as CLR profiler In order to use CLR profiler i have created as simple windows application here In this windows application I have created a simple class here called as CLS1 This class does not do anything as such, it just a class here just create some 1000 or 2000 objects In the button click I am just looping 1000 times and I creating objects Build this application Go to the CLR Profiler The CLR profiler is a tool which you can get from Microsoft.com If you go to Microsoft.com and search for CLR profiler you will get to downloads like 64 bit and one is for 86 Take whichever is appropriate for your environment Run this profiler and browse to the application and pickup my application here Create the 1000 object It will create the 1000 objects and close this If you go to objects by addresses you will see nice visual graph here the generation 0 objects We have created 1000 objects and all those 1000 objects are in generation 0 at this moment In this way you can view how many objects are allocated in which generations If you want to see more view of your objects Adjust your readings accordingly to see a better view of it By using the CLR profiler you can get idea of how many objects are created in generation 0 and generation 1 and generation 2 The most important point to remember is more the objects in generation 0 the better it is The objects are created and the memory is claimed The more the objects are in generation 1 and generation 2 the worst it is because that means the memory is getting allocated but it is never getting cleaned up or never getting reclaimed Until your .NET application runs that memory cannot be claimed back Healthy application sign atleast for the managed memory .NET manage memory The most of the object should be created in generation 0 rather than generation 1 and generation 2

27 thoughts on “.NET C# Interview questions and answers :- What is Garbage Collector, Gen 0, 1 & 2?

  • how does it know the two objects are not needed…is there a counter or a lifetime property that is set and expires to determine whether an object is not needed

  • @pagola Its look at the scope for example , the below customer object will expire after the last cusrly bracket.
    void SomMethod
    {
    Customer o = new Customer()
    }// This curly bracket is the scope

  • @dnfvideo i do not understand …..why would it expire after the last command when it has just been created? an instance of Customer has just been created ? before the garbage collector puts objects into the generations , how does it tell an object is no longer in use

  • @dnfvideo The garbage scans every now and then your .NET application. If he finds the object is out of scope i.e. the } is executed , he marks them from cleaning.

  • Why do we need 3 Gen?
    If you have many(thousands) of objects. Trying to figure out if they are needed is a bulky process
    Gen0 is updated the most, Gen1 less, and Gen2 even less
    It decreases the amount of objects to verify at each pass, pro: decrease time, con: increase space usage

    With the CLRProfiler tool (download form microsoft) you can visualize the Gen0,1,2 of the garbage collector

  • Garbage collector is a background thread that cleans unused managed objects
    Everything OUtside the CLR boundary(unmanaged objects) can not be removed by Garbage collector

    GC0 – Gen 0 – Objects newly created are inserted into GC0 memory
    GC1 – Gen 1 – Objects that survived the first clean up are moved to GC1 memory
    THe other objects are deleted/discarded from GC0
    GC2 – Gen 2 Objects that survivedd the Gen1 clean up are copied to Gen2

  • In the end of video it is mentioned that most of the objects should be created in gen1 or gen2. How do we promote objects from gen0 to gen1 or gen2?

  • Does this imply that it's better to use local and not member variables/objects when I have the chance to do so? (like when I have a variable that I'm only using once inside a method)

  • Hi Thanks..just Quick Question —When will be GC Triggered in .net Application do we have a CLR configuration as in JVM …wherein we can set the different Gc policies Like CMS initiating occupancy

  • alot of missing stuff from this explination but anyway, you said the GC checked the g0 and the g1 why? the virtual mem reached its limit, GC runs for just g0, if it managed to leave the limit, it wont go for the g1 and g2 right?

  • @3:41.. Dude.. You're just throwing crap and I cant believe all these guys here are just sitting here and taking it! Just came here accidentally btw. FYI, the GC doesn't JUST move objects from gen 0 to Gen 1 lol.. There's a reason behind it and it's called available memory.. which comes into play when you're instantiating a new object. and there isn't much space available.. Wow.. Unbelievable!

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *