GC.GetGeneration might return Int32.MaxValue

Starting in .NET 8, GC.GetGeneration might return Int32.MaxValue for objects allocated on non-GC heaps (also referred as "frozen" heaps), where previously it returned 2. When and how the runtime allocates objects on non-GC heaps is an internal implementation detail. String literals, for example, are allocated on a non-GC heap, and the following method call might return Int32.MaxValue.

int gen = GC.GetGeneration("string");

Previous behavior

Previously, GC.GetGeneration returned integer values in the range of 0-2.

New behavior

Starting in .NET 8, GC.GetGeneration can return a value of 0, 1, 2, or Int32.MaxValue.

Version introduced

.NET 8 Preview 4

Type of breaking change

This change is a behavioral change.

Reason for change

.NET introduced a new, non-GC kind of heap that's slightly different from the existing heaps, which are large object heap (LOH), small object heap (SOH), and pinned object heap (POH).

Make sure you're not using the return value from GC.GetGeneration() as an array indexer or for anything else where Int32.MaxValue is unexpected.

Affected APIs