Garbage collector

Question posted on 10 2010
Rate question difficulty level 1 Votes
How the garbage collector works internally tell step by step?
 
 
3 Answers
 
In the common language runtime (CLR), the garbage collector serves as an automatic memory manager. It provides the following benefits:

Enables you to develop your application without having to free memory.

Allocates objects on the managed heap efficiently.

Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field.

Provides memory safety by making sure that an object cannot use the content of another object.
-----------------------------------------------------------------
What Happens During a Garbage Collection?

A garbage collection has the following phases:

A marking phase that finds and creates a list of all live objects.

A relocating phase that updates the references to the objects that will be compacted.

A compacting phase that reclaims the space occupied by the dead objects and compacts the surviving objects. The compacting phase moves objects that have survived a garbage collection toward the older end of the segment.

read more and credits : http://msdn.microsoft.com/en-us/library/ee787088.aspx

10/14/2010
 
 
• Garbage Collection works like below.
1. Garbage Collection has 3 generations. 0,1,2.
2. Memory allocation happens from 0 generation.
3. Lower the memory address higher the generation.
4. .NET Runtime checks at generation 0 and identifies the dead addresses in the memory and collects them (means clears them). The checking happens as an hierarchy. (From root address to the hierarchy).
5. If generation 0 fills with all the live adresses then to accommodate new memory allocations, addresses in the generation 0 will be moved to generation 1 and frees the generation 0 with some space.
6. If generation 1 filled with the addresses, then .NET rutime moves the addresses locations to generation 2.
7. So, Lower the address number higher the life of the memory location.
9. Lower the address number higher the generation number.
10. So, all the initial memory allocations will happen at generation 0 only.
11. But we may get a question that, if generation 2 also filled up then what is the solution. Needs to increase the Physical Memory.


10/17/2010
 
 
Your explanation is almost correct however there are a couple of important issues.

Promotion of memory allocations runs after each garbage collection operation. Any allocated memory in Gen 0 is marked as Gen 1 after a single garbage collection, not when Gen 0 is full.

Also there is a large object heap that is seperate to the generations, I cannot remember the defaults, but any object that is over the default for memory consumption is placed onto the large object heap, seperate to the main memory allocation and garbage collected independantly.

As for the question of what happens when Gen 2 fills up is the system will either go into repeated garbage collection operations as it tries to clear more memory or in extreme cases a Out of Memory expception occurs. Performance monitoring can help to identify memeory issues in dotNET applications.

Adding more physical memory may not help the situation, depends on the paltform (x86 or x64) limitations. Better to solve the undelying reason as to why memory consumption is so high in the first place.


10/17/2010
 
 
Add an answer*
 
Your name
Email
 
Location: delhi , India

add a question

arrow_blue


Now hiring!
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------