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 One and
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.krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator