How garbage collector identifies the objects which are not in use in c#

Posted by krrishbiju-15589 under C# on 9/9/2013 | Points: 10 | Views : 1789 | Status : [Member] | Replies : 3
Hi,
Anyone please help me on this question.

How garbage collector identifies the objects which are not in use in c#.
Regards
krrish




Responses

Posted by: Nismeh on: 9/9/2013 [Member] Starter | Points: 25

Up
0
Down
When an program is loaded in the memory there will be a bunch of memory allocated for that particular program alone and loaded with the memory. This bunch of memory is called Managed Heap in .NET world. This amount of memory will only be used when an object is to be loaded in to the memory for that particular program.

This memory is separated in to three parts.

Generation Zero.
Generation One and
Generation Two.

Ideally Generation zero will be in smaller size, Generation one will be in medium size and Generation two will be larger.

When we try to create an object by using NEW keyword in the high level languages. It will simply emit newobj in to the MSIL file. (newobj is a Microsoft Intermediate Language instruction to create a new type). When newobj executes, the system will,

Calculate the number of bytes required for the object or type to be loaded in to the managed heap.
Add the bytes required for an object’s overhead. Each object has two overhead fields: a method table pointer and a SyncBlockIndex. On a 32-bit system, each of these fields requires 32 bits, adding 8 bytes to each object. On a 64-bit system, each is 64 bits, adding 16 bytes to each object.
The CLR then checks that the bytes required to allocate the object are available in the reserved region (committing storage if necessary). IF the object fits, it is allocated at the address pointed to by NextObjPtr. The type’s constructor is called (passing NextObjPtr) for the this parameter), and the newobj MSIL instruction (or the new operator) returns the address of the object. Just before the address is returned, NextObjPtr is advanced past the object and indicates the address where the next object will be placed in the heap.

When Generation Zero is full and it does not have enough space to occupy other objects but still the program wants to allocate some more memory for some other objects, then the garbage collector will be given the REALTIME priority and will come in to picture.

Now the garbage collector will come and check all the objects in the Generation Zero level. If an object’s scope and lifetime goes off then the system will automatically mark it for garbage collection.

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: krrishbiju-15589 on: 9/9/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Nismesh,
Thanks for your reply.
But i want to know how the garbage collector identifies the scope and life time of an object in gen0,1,2.
Regards
krrishbiju

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://stackoverflow.com/questions/5422918/how-does-garbage-collection-and-scoping-work-in-c
http://accu.org/index.php/journals/244
http://blogs.msdn.com/b/abhinaba/archive/2009/01/30/back-to-basics-mark-and-sweep-garbage-collection.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response