Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I was going through the fundamental of Garbage collection from the following URL

http://msdn.microsoft.com/en-us/library/ee787088.aspx[^]

During going through the article, it became quite prominent that the Garbage collection works on two different sets of objects (Short Lived and Long Lived) which are distributed among Generations (Generation 0 to 2).

I was unable to understand the meaning of Short Lived and Long lived objects. It will be really helpful, if someone helps me out in this.
Posted

Objects that are not released for GC during a Gen-0 cycle get promoted to Gen-1, and similarly if they are not released for GC during the next Gen-1 cycle, they are promoted to Gen-2. Such objects are typically referred to as long lived objects. Usually these objects are alive throughout the application's lifetime.
 
Share this answer
 
Comments
senguptaamlan 19-Oct-10 8:38am    
yeah you are very true..but I'm not getting who promotes this objects to higher generations or tags those objects as Long Lived objects? Is it the CLR or the Garbage collector.
Nish Nishant 19-Oct-10 8:45am    
The generation promotions are performed by the CLR's Garbage Collector. There is no tagging of any sort. By virtue of being in a higher gen, an object is deemed to be long-lived (again there is no technical definition for what makes an object long lived).
In addition to what Nish says, as the article suggests,

"Most objects are reclaimed for garbage collection in generation 0 and do not survive to the next generation.

Generation 1. This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.

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.

Garbage collections occur on specific generations as conditions warrant. Collecting a generation means collecting objects in that generation and all its younger generations. A generation 2 garbage collection is also known as a full garbage collection, because it reclaims all objects in all generations (that is, all objects in the managed heap).
Survival and PromotionsObjects that are not reclaimed in a garbage collection are known as survivors, and are promoted to the next generation. Objects that survive a generation 0 garbage collection are promoted to generation 1; objects that survive a generation 1 garbage collection are promoted to generation 2; and objects that survive a generation 2 garbage collection remain in generation 2.
When the garbage collector detects that the survival rate is high in a generation, it increases the threshold of allocations for that generation, so the next collection gets a substantial size of reclaimed memory. The CLR continually balances two priorities: not letting an application's working set get too big and not letting the garbage collection take too much time.".



So anything surviving till GC2 is a long-lived object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900