0% found this document useful (0 votes)
16 views2 pages

Garbage Collection

The document explains garbage collection in the .NET framework, which is an automatic memory management service that cleans up resources for managed objects in the managed heap. It describes the concept of generations, including Generation 0 for short-lived objects, Generation 1 as a buffer, and Generation 2 for long-lived objects, along with the steps involved in the garbage collection process. Additionally, it outlines the role of application roots in determining when objects are no longer needed by referencing various storage locations that contain object references.

Uploaded by

loginshiv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Garbage Collection

The document explains garbage collection in the .NET framework, which is an automatic memory management service that cleans up resources for managed objects in the managed heap. It describes the concept of generations, including Generation 0 for short-lived objects, Generation 1 as a buffer, and Generation 2 for long-lived objects, along with the steps involved in the garbage collection process. Additionally, it outlines the role of application roots in determining when objects are no longer needed by referencing various storage locations that contain object references.

Uploaded by

loginshiv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Garbage Collection:

 In .NET framework, garbage collection is an automatic memory management service


that takes care of the resource cleanup for all managed objects in the managed heap.
 When garbage collector is initialized by the CLR, it stores and manages objects by
allocating a segment of memory called the managed heap.
 Each managed process in .NET has a managed heap. Each thread in a process shares
the same managed heap to store and manage objects.
 When garbage collector runs, it removes dead objects (object that has no reference in
memory or has the value null) and reclaims their memory; it compacts the living objects
together to preserve their locality and makes the managed heap smaller.

Generations:

GC supports the concept of generations. It helps to organize short-lived and long-lived objects
in a managed heap.

There are three generations:

1. Generation 0

2. Generation 1

3. Generation 2

1. Generation 0:
When an object is allocated on the Heap, it belongs to generation 0. It is the young
generation, which contains short-lived objects like temporary variables. If newly
allocated objects are larger in size, they will go on the large object heap in a generation
2 collection. GC occurs mostly in generation 0.

2. Generation 1:
When objects survive from a garbage collection of generation 0, they go to generation
1. Objects in generation 1 serve as a buffer between short-lived and long-lived objects.
3. Generation 2:
When objects survive from a garbage collection of generation 1, they go to generation
2. Objects in generation 2 serve as long-lived objects. If objects still survived in
generation 2, they remain in generation 2 till they’re alive.

Steps involved in Garbage Collection process:

1. Suspend all managed threads except for the thread that triggered the garbage collection.

2. Find a list of all living objects.

3. Remove dead objects and reclaim their memory.

4. Compact the survived objects and promote them to an older (higher) generation.

The Role of Application Roots:

How the garbage collector determines when an object is no longer needed. To understand the
details, you need to be aware of the notion of application roots. Simply put, a root is a storage
location containing a reference to an object on the managed heap. Strictly speaking, a root can
fall into any of the following categories:

 References to global objects (though these are not allowed in C#, CIL code does permit
allocation of global objects).

 References to any static objects/static fields.

 References to local objects within an application's code base.

 References to object parameters passed into a method.

 References to objects waiting to be finalized.

You might also like