Game Development Reference
In-Depth Information
Overview of the Garbage Collector
The CLR garbage collector is a generational mark-and-compact collector (also
known as an ephemeral garbage collector), offering excellent performance and effi-
ciency by taking some fundamental notions into consideration. The primary notion
is that short-lived objects tend to be smaller and are accessed more often. The GC
divides the memory allocation graph into sub-allocation graphs titled generations ,
each with a specific purpose. The three generations are shown in Table 34.1.
Table 34.1 CLR Garbage Collector Generations
Generation
Description
Generation 0 ( Gen0 )
This generation contains newly allocated objects that are frequently
used. This generation is typically the smallest in size, taking roughly 10
milliseconds to collect.
Generation 1 ( Gen1 )
This generation is for larger and older objects that are used
infrequently. When Gen1 is collected, Gen0 is collected as well.
Generation 2 ( Gen2 )
This generation is for larger and older objects that are used
infrequently, except it is also a full collection that can be optimized
for intelligent CPU caching by the underlying system if supported.
The managed heap originally starts in an empty state. Objects that are allocated
initially go into the generation 0 portion. When a collection occurs, the GC deter-
mines which objects are garbage (no more references pointing to them) and which
objects are surviving and need to be compacted . When an object is compacted, it
moves into an older generation.
Currently there are three generations, but you can query the maximum number
of generations if needed. You do so by getting the value from the GC.MaxGeneration
property, which will always return 2 . This may change in future versions of the
GC. Any objects in generation 2 that get compacted will remain in generation 2.
More information on interacting with the garbage collector is covered later in this
chapter.
The main advantage of a generational garbage collector is that collections of a por-
tion of the heap take less time than collecting the entire heap. The garbage collector
can choose to examine only objects in generation 0, and because it mostly contains
objects with a short life span, there is a good chance that a lot of memory will be
reclaimed without the need to examine other generations.
Search Nedrilad ::




Custom Search