-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
Description
The following GC
APIs:
int GetGeneration(object obj);
int GetGeneration(WeakReference wo)
may return int.MaxValue
for objects allocated on non-gc heaps (also referred as "frozen") where previously it used to return 2
. When and how the runtime allocates objects on non-GC heaps is an internal implementation detail. A good example are most string literals, so e.g.
int gen = int GetGeneration("string");
might return int.MaxValue
.
Version
.NET 8 Preview 4
Previous behavior
GC.GetGeneration
used to return values in the range [0, 1, 2]
.
New behavior
GC.GetGeneration
returns values in the range [0, 1, 2, int.MaxValue]
.
Type of breaking change
- Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
- Behavioral change: Existing binaries may behave differently at run time.
Reason for change
Introduction of a new kind of heap (NonGC heap) that is slightly different from the existing ones (SOH, POH, LOH). More details can be found here dotnet/diagnostics#4156
Recommended action
Make sure GC.GetGeneration
's return value is not used as an array indexer or any other case where int.MaxValue
is not expected
Feature area
Core .NET libraries
Affected APIs
On the class System.GC
:
int GetGeneration(object obj);
int GetGeneration(WeakReference wo)