GC Generations In C#
The heap is organized into generations so it can handle long-lived and short-lived objects. Garbage collection primarily occurs with the reclamation of short-lived objects that typically occupy only a small part of the heap. There are three generations of objects on the heap:
Generation is three type
1. Generation "0".
2. Generation "1"
3. Generation "2"
Explanation :
-
Generation 0. This is the
youngest generation and contains short-lived objects. An example of a
short-lived object is a temporary variable. Garbage collection occurs
most frequently in this generation.
Newly allocated objects form a new generation of objects and are implicitly generation 0 collections, unless they are large objects, in which case they go on the large object heap in a generation 2 collection.
Most objects are reclaimed for garbage collection in generation 0 and do not survive to the next generation.
Example :
Bellow example showing GC Generation "0", these values are short live values.
public void TestGC()
{
int a =0;
string testvalue=string.empty;
testvalue obj=new testvalue();
}
-
Generation 1. This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
Example :
Bellow example showing GC Generation "1", these values are not short live object and long live object. means these objects are middle layer object.
public void TestGC()
{
int a =0;
string testvalue=string.empty;
testvalue obj=new testvalue();
obj.functionName();//its work like until and unless completing function this memory will storing in GC Generation "1" Place.
}
-
Generation 2. This
generation contains long-lived objects. An example of a long-lived
object is an object in a server application that contains static data
that is live for the duration of the process.
Example :
Bellow example showing GC Generation "2", these values long live object. when we closing our application then only these object release in memory.
Project main function.
Public static void main(string[] arg)
{
this type of object will store in GC generation "2".
//to do.
}
Because objects in generations 0 and 1 are short-lived, these generations are known as the ephemeral generations.
Ephemeral generations must be allocated in the memory segment that is known as the ephemeral segment. Each new segment acquired by the garbage collector becomes the new ephemeral segment and contains the objects that survived a generation 0 garbage collection. The old ephemeral segment becomes the new generation 2 segment.
The ephemeral segment can include generation 2 objects. Generation 2 objects can use multiple segments (as many as your process requires and memory allows for).
The amount of freed memory from an ephemeral garbage collection is limited to the size of the ephemeral segment. The amount of memory that is freed is proportional to the space that was occupied by the dead objects.
Please check these links
http://www.microsoft.com/en-in/download/details.aspx?id=14727.
0 comments:
Post a Comment