Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Breaking change]: GC.GetGeneration may return int.MaxValue for certain types of objects #35105

@EgorBo

Description

@EgorBo

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)

Associated WorkItem - 91241

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 8Work items for the .NET 8 release📌 seQUESTeredIdentifies that an issue has been imported into Quest.breaking-changeIndicates a .NET Core breaking change

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions