Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .NET, Is GC.MaxGeneration Ever Not 2?

As far as I understand it, there are three generations of garbage collection - 0, 1 and 2, for all versions of .NET.

Are there any circumstances where examining GC.MaxGeneration would yield anything other than 2? Are there GCs on other frameworks that have a different number of GC generations?

like image 451
James Wiseman Avatar asked Apr 05 '13 12:04

James Wiseman


1 Answers

It should be noted that GC.MaxGeneration being constant for the application's lifetime is in itself an implementation detail:

Notes to Implementers

For this implementation, the value returned by the MaxGeneration property is guaranteed to remain constant for the lifetime of an executing application.

This implies that not only can MaxGeneration be different from 2 in other implementations of the .NET framework, but it can also vary during program execution (depending on the number of generations used by the garbage collector at the time the property getter is called).

Other implementations can also use different garbage collectors depending on the circumstances. For instance, Mono can either use the Boehm non-generational GC (MaxGeneration will always be 0 then), or the SGen generational GC (which only implements two generations, so MaxGeneration will always be less than or equal to 1 there).

like image 105
Frédéric Hamidi Avatar answered Oct 08 '22 18:10

Frédéric Hamidi