Garbage Collector in Asp.Net - ProgramIdea

Garbage Collection in C#


Unused object automatically released by automatic garbage collection in Asp.Net. Garbage collector executes automatically when system has low physical memory.

Advantage of Garbage Collector

1. No worry about memory management

2. Allocate object memory on managed heap efficiently.

3. Reclaims objects that no longer being used, clears their memory and keeps the memory for future allocations.

4. Provides memory safety by making sure that object cannot use the content of other object.

How Garbage Collector works

Garbage collector initialized by CLR and allocate memory for object, this memory is known as managed heap. Managed heap organized into form of Generation that contains reference of objects. Garbage Collector divides complete managed heap into three generation as:

Generation 0

Generation 1

Generation 2

Generation is a portion of memory from managed heap that handle long-lived and short-lived objects. Generation 2 memory size is always greater than generation 1 memory and generation 1 memory is always greater than generation 0 memory.

When application starts execution then always newly created object comes inside generation 0.

Let's understand how internal process going on...

Process first

Whenever new object is created, it comes into generation 0.

When Generation 0 is completely filled and application is trying to create a new object then:

# Garbage collector perform an operation known as collection means.

# Garbage collector examines all the object that present in generation 0, identifies idled objects and objects in use.

# Idled objects are destroyed or placed in finalization queue.

# Objects in use that survive in generation 0 and promoted to next generation i.e. generation 1 so that generation 0 will become empty.

# Now again newly created object place inside generation 0.

# After some performing collection generation 0 and generation 1 will be completely filled.

Process second

When generation 0 and generation 1 completely filled and application is trying to create a new object then:

# Garbage collection again perform the operation collection means.

# Examines all the object present in generation 0 and generation 1, identifies idled objects and objects in use.

# Idled objects are destroyed or placed in finalization queue.

# Objects in use that survive in generation 1 and promoted to the next generation i.e generation 2, so that generation 0 and generation 1 will become empty.

# Now again newly created object will be placed in generation 0.

# After some performing so many collections generation 0, generation 1 and generation 2 will be completely filled.

Process third

When generation 0, 1, 2 are completely filled and application is trying to created a new object then:

# Garbage collector again perform the operation collection means.

# Examine all the objects in generation 0, generation 1 and generation 2.

# Divided object into idled objects and objects in use.

# Idled objects are destroyed or place in Finalization queue.

# Objects in use that survive in generation 2 remain in generation 2.

# Now newly created object placed inside generation 0.

# Generation 2 is also known as full garbage collection, because it reclaims all objects from all generations.

Performing the collection of generation 0 garbage collector will take 1/10th of a nano second time which is less than a page cycle.

How garbage collector identify that objects in use or not

Garbage collector use following information to determine whether objects are in use or not:

1. Stack roots - Stack variables provided by JIT compiler and stack walker.

2. Garbage collection handler - Handles that points to managed object and can be allocated by user code or by CLR.

3. Stack data - Static object in application domain that could be referencing other objects. Each application domain keep track of its static objects.