c# - 为什么垃圾收集器只有 3 代,不多也不少?

标签 c# .net

因为我经历了垃圾收集 (GC) 及其代(0,1,2)。 我突然想到一个问题,为什么 Microsoft 决定将其保留为 3?

为什么不要少于 3 或多于 3?我需要一些合乎逻辑的解释。

最佳答案

引用文档,

Generations

The heap is organized into generations so it can handle long-lived and short-lived objects. Garbage collection primarily occurs with the reclamation of short-lived objects that typically occupy only a small part of the heap. There are three generations of objects on the heap:

Generation 0.

This is the youngest generation and contains short-lived objects. An example of a short-lived object is a temporary variable. Garbage collection occurs most frequently in this generation.

Newly allocated objects form a new generation of objects and are implicitly generation 0 collections, unless they are large objects, in which case they go on the large object heap in a generation 2 collection.

Most objects are reclaimed for garbage collection in generation 0 and do not survive to the next generation.

Generation 1.

This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.

Generation 2.

This generation contains long-lived objects. An example of a long-lived object is an object in a server application that contains static data that is live for the duration of the process.

Garbage collections occur on specific generations as conditions warrant. Collecting a generation means collecting objects in that generation and all its younger generations. A generation 2 garbage collection is also known as a full garbage collection, because it reclaims all objects in all generations (that is, all objects in the managed heap).

我们可以看到,第 2 代 适用于应在整个过程中持续存在的对象。为比进程生命周期更长的对象建模是不明智的,至少在我所知道的操作系统上是这样。

我怀疑没有更大的世代粒度,因为它会证明比派生的好处更麻烦。对这个概念的任何推断都是纯粹的猜测,没有进行真实世界的性能测试。

我怀疑即使是来自 GC 设计者的规范答案也容易引起猜测,但他们可能能够详细说明该粒度级别是如何计算的。

关于c# - 为什么垃圾收集器只有 3 代,不多也不少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57953646/

相关文章:

c# - 在 C# 中调用 C++ dll 时找不到入口点

c# - 我可以从非虚拟化应用程序访问 Windows 虚拟商店中的文件和注册表项吗?

c# - 将 Shift+1 检测为 Key.Add

.net - 为什么.NET中8号字体显示为8.25?

.NET vs ASP.NET vs CLR vs ASP

c# - .NET System.Diagnostics.Stopwatch 问题(返回值太低)

.net - 随机 "could not establish secure channel for SSL/TLS"错误

c# - 防止 Winforms Designer 为继承的控件生成属性值

c# - 使用反射计算代码行数的简单方法

c# - ssl webservice中的自定义用户名/密码