Note 1 : It is good to read my article about Garbage collection before reading this article
We know that what is garbage Collection is. Now we going to working of Garbage collection (ie it this article , actually I am explaining life time of object) .It mainly containing four steps,
1)Allocation 2)Mark 3)Generation 4)Compact
1. Allocation
String Name = new string (); When executing above statement. CLR allocate memory space for above Name object in managed heap (if you want Know more about Heap please "Click". In this process system as to make sure that there’s enough space in managed heap. If there is no enough space in heap CLR Throw OutOfMemoryExcptiopn
2. Mark
Marking is the finding of the un referenced object in the managed heap or Finding of memory that can be reclaimed. When the garbage collector starts running. It makes the assumption that all objects in the heap are garbage (A object it all reference is disappeared is called Garbage). In other words, it assumes that none of the application's roots refer to any objects in the heap. Note 2: Actually we don’t know when the garbage collection take place. It’s algorithm may change in different version of .Net framework; Theoretical collection is take place when heap is full.
Note 3: We can also manually start collection by calling function System.GC.Collect. But use it is not recommended in everywhere.
Then GC starts walking the roots and then identifies all objects reachable from the roots or all object having reference in heap. These objects are called Live Object . If the GC find an object already identified. Then it stops walking down that path. This serves two purposes. First, it helps performance significantly since it doesn't walk through a set of objects more than once. Second, it prevents infinite loops should you have any circular linked lists of objects. Thus cycles are handles properly
Once all the roots have been checked, then GC considered all other objects not identified by it as garbage.
3 Generation
It’s costly to walk though all object that are currently referenced. Much of it working in doings this will be wasted. To avoid this GC uses a mechanism Called Generation. That is Garbage collector takes into account two facts that have been empirically observed in most programs in a variety of languages: 1. Newly created objects tend to have short lives. 2. The older an object is, the longer it will survive. To solve this issue GC divides the object in to three generation 1) 0-Genration: Which are objects newly allocated, Object that have never been considered for collection 2) 1-Genration: Which are the objects survived from a single garbage collection 3) 2-Genration: Which are the objects survived from a more than one Garbage collection In the next marking phase, it first walks through only 0-Genration objects. If it is not getting required space then it walks though 1-Genration and 2-Genration
4 Compact
In Compact phase freeing of memory take place. Move all the live objects to the bottom of the heap, leaving free space at the top. The garbage collector now walks through the heap linearly, looking for contiguous blocks of garbage objects (now considered free space). The garbage collector then shifts the non-garbage objects to down in the memory, removing all of the gaps in the heap. Moving the objects in memory invalidates all pointers to the objects. So the garbage collector modifies the application's roots so that the pointers point to the objects' new locations
After all the garbage has been identified, all the non-garbage has been compacted, and all the non-garbage pointers have been fixed-up, a pointer is positioned just after the last non-garbage object to indicate the position where the next object can be added.
Thanks Psaj,nitc
Note 4: Algorithm used in Garbage collection is known as mark and compact Algorithm
|
| Author: critic 25 May 2004 | Member Level: Bronze Points : 0 |
Sajid,
It is good to see self written articles. But please check the spelling - there are several spelling mistakes. The one line of the 'summary' part itself has many spelling mistakes. Please copy and paste your article to MS Word - it will show you all the spelling mistakes.
Also, please use proper spacing - give one and only one blank line after each paragraph. In some places you have used more than one blank line and in some places no blank lines.
Correct these mistakes and your article will be good to go!
|
| Author: Payal Shetty 29 Jun 2004 | Member Level: Bronze Points : 0 |
I am new to .NET and I found this site during one of my frequent searches on google. Since then, I have been a regular visitor. The articles are simple and the flow is lucid. But if you could check the grammar it would make the reading a lot easier. The above article was good but the "Generation" section was not clear. A better explanation on that would be appreciated. Thanks!
|
| Author: Payal Shetty 29 Jun 2004 | Member Level: Bronze Points : 0 |
Ah! I forgot to add this in my previous post. It would also be appreciated if you dated the article coz it would give the reader an idea as to how "recent" or how "up-to-date" the matter in the article is. Thanks!
|
| Author: dsm 23 Aug 2005 | Member Level: Bronze Points : 0 |
great article.. i have been searching on the web and finally i get a chance to read a good overview of GC, other then spelling good job
-Mohammed
|