Mutation Person
Mutation Person

Reputation: 30498

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?

Upvotes: 7

Views: 692

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

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).

Upvotes: 9

Related Questions