How the garbage collector knows when to collect garbage ?

 Posted by Akiii on 5/7/2011 | Category: .NET Framework Interview questions | Views: 7457 | Points: 40

The garbage collector's optimizing engine determines the best time to perform a collection based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

Source: | Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Kishork80 on: 5/7/2011 | Points: 10
GC keeps a list of all Strong Reference objects and Weak Reference objects used by the application at Run time. It collects Weak Reference objects when not in use by the Appication. It gives another chance or keep an eye on Strong Reference objects as well. As soon as Strong Reference objects are not being used then the same is moved to Weak Reference objects . hence GC collects it while cleaning memory.
Posted by: Akiii on: 5/7/2011 | Points: 10
hi sir,

Thanks for the information

Posted by: Lokesh76 on: 5/7/2011 | Points: 10
Garbage collection occurs when one of the following conditions is true:

1. The system has low physical memory.

2. The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.

3. The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

Posted by: Akiii on: 5/7/2011 | Points: 10
hi lokesh.....

thanks for providing more information......

But how is this managed heap threshold there is any fixed way by which the framework or the compiler does that....?

Thanks and Regards
Posted by: Lokesh76 on: 5/7/2011 | Points: 10
Hi Akiii,

There is nothing fixed but yes by default on 32 bit computer for each process has 2 GB virtual address space but nothing fixed threshold adjusted based on process runs on computer.

Posted by: Akiii on: 5/7/2011 | Points: 10
hi lokesh...

i think we can adjust that virtual memory also...manually!

thank you for ur answer....
Posted by: Chvrsri on: 5/9/2011 | Points: 10
Dear Akii and Lokesh ,

I really appreciate that a healthy and technical discussion is going on, but my small request to both of you,

@ Akii :- If you are interested in asking questions relating to GC kindly ask this in forums section where it will be useful for all.

@Lokesh :- If you have more info regarding GC post some nice questions we shall approve it.

Please take it in a positive way. We want every thing to be well organized i mean asking questions only in forums section(So that every one can see this discussion and learn new things.It is difficult for users to get information from interview questions responses), So kindly follow this simple steps and gain knowledge.

Thank you for posting at DotNetFunda !!!
Posted by: Akiii on: 5/9/2011 | Points: 10

Sorry sir.......yes you are right.....It will not happen again.....
I am very sorry for that......

Posted by: Chvrsri on: 5/9/2011 | Points: 10
Hi Akii,

That's OK ,Never Mind. These are not rules but just only good practices . You can call me Srikanth.


Login to post response

More Interview Questions by Akiii