Garbage Collection in .NET

Fundamentals of Garbage Collector ” In CLR- GC work as an automatic memory Manager“.

GC Do:

Allocate object on the managed heap efficiently.

Reclaim object that no longer being used and keep memory available for future allocations.

Provide memory safety i.e. an object cannot use the content of another object.

Fundamentals of Memory:

Each process has its own separated address space.

All processes on the same computer share the same physical memory, and share the same page file if there is one.

By default: a 32 bit computer, each process has a 2 GB user mode virtual address space.

If you write native code we use Win32 functions to work with the virtual address space. These function allocate and free virtual memory for you on native heaps.

Virtual Memory: 3 states

Free. No reference to it

Reserved. Available for use.

Committed. Assigned to physical storage.

Virtual address cannot be fragmented means memory manager has to find a single free block.

** You will run out of memory if you run out of virtual address space to reserve or physical space to commit.

Garbage collection occurs when:

Low physical memory.

Memory allocated objects on managed head surpass an acceptable threshold.

GC.Collect method– for unique situation and testing.

Managed Heap:

After the GAC initialized by CLR. It allocate a segment of memory to store and manage objects called managed Heap.

Opposed to native heap.

Each process have a managed heap and all thread in the process allocate memory for objects on the same heap.

To reserve memory the garbage collector calls the Win32 VirtualAllocfunction.

Also releases segment back to the OS calling VirtualFree.

When GAC triggered the GAC reclaim the memory from dead objects.

Large & Small object heap.


Heap is organized into generations for handing short/long lived objects.

Generation of objects on the heap:

0: Youngest contain short lived objects(temp objects).

Garbage collection occurs more frequently.

1: Buffer

2: Long lived ex an object in server application

That contain static data that is live for the duration of the process.

In garbage collection in generation 2 : Full garbage collection reclaiming all objects in managed heap.

Ephemeral(short time) segment: Generation 0 & 1 objects

Must be allocated in the memory segment depend upon system bit and type of GC.

System 32 bit/64

workstation GC -16MB/256MB

Server GC64MB/2 GB

Amount of memory directly proportional to space occupied by dead object.

During a garbage Collection:

Marking of live objects.

Relocating /update object reference.

Compacting— Reclaim space occupied by dead object.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s