第三代对象和大对象堆的区别

发布于 2024-10-15 05:19:43 字数 25 浏览 6 评论 0原文

大对象堆和GC第三代对象有什么区别?

What is the difference between large object heap and GC 3rd generation objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2024-10-22 05:19:43

LOH(大对象堆)是一个单独的堆,其中大对象被直接分配并保留在那里直到被收集。对象根据其大小(例如等于或大于85000 字节)直接分配到LOH 中。

分代对象是分配到 SOH(小对象堆)中的“小”对象,SOH 是单个堆。 SOH 中的对象有一个关联的世代,表示它们在最大世代(例如 2)之前存活了多少个集合。由于世代编号从 0 开始,第 2 代中的对象可以被描述为第三代,因为它至少存活了3 个系列,即第 0、1、2 代。

Generations 有助于优化垃圾扫描。长寿对象的代数随着它们在集合中的存在而增加,并且代数越高,扫描的频率就越低。这种机制会导致非短期对象的扫描频率降低,因此没有必要。分代方案应用于 SOH,因为它被视为对存在大量对象的堆的良好优化。

更新

据我了解,LOH 对象被报告为处于最大生成中,但我相信这只是一个默认值。它们实际上并不属于任何一代,即第 2 代 SOH 对象和 LOH 对象不在同一“列表”中。然而,正如@Henk所指出的,在执行第2代收集时,此时也会收集LOH对象。因此,从概念上讲,第 2 代和 LOH 之间存在某种关系。从 .Net 2.0 开始,这是正确的:

请参阅:大对象堆揭秘

从生成的角度来看,大对象属于第 2 代,因为它们仅在存在第 2 代收集时才会被收集。

然而,尽管集合关系很明显,但它不成立的一个例子是生成压缩。当一代被收集时,它也可能被压缩。然而,LOH 并未被压缩,因此不能说发生在第 2 代对象上的所有事情都会发生在 LOH 中的对象上。

[Test]
public void large_object_heap_objects_are_reported_as_max_generation()
{
    int[] bling = new int[85000 / 4];

    int maxGen = GC.MaxGeneration;
    int objectGen = GC.GetGeneration(bling);

    Assert.AreEqual(maxGen, objectGen, "Large object is at max generation.");
}

The LOH (Large Object Heap) is a single heap where large objects are allocated directly and stay there until they are collected. Objects are directly allocated into the LOH based on their size e.g. being equal or greater than 85000 bytes.

Generational objects are "small" objects that are allocated into the SOH (Small Object Heap) which is a single heap. Objects in the SOH have an associated generation which denotes how many collections they have survived up to the maximum generation e.g. 2. As the generation number starts at 0, an object in generation 2 could be described as 3rd generation as it has survived a minimum of 3 collections i.e. generations 0,1,2.

Generations helps to optimize garbage scanning. Long lived objects have their generation number increased as they survive collections, and generations with a higher number are scanned less frequently. This mechanism results in objects that are not short-lived being scanned less frequently and therefore unnecessarily. The generational scheme is applied to the SOH as it seen as a good optimization for a heap where there will be lots of objects.

Update

As far as I understand LOH objects are reported as being in the max generation, but I believe that this is just a default value. They are not actually in any generation i.e. generation 2 SOH objects and LOH objects are not in the same "list". However, as pointed out by @Henk, when performing a generation 2 collection, LOH objects are also collected at this time. So conceptually there is a relationship between generation 2 and the LOH. This is correct as of .Net 2.0:

See: Large Object Heap Uncovered

From a generation point of view, large objects belong to generation 2 because they are collected only when there is a generation 2 collection.

However, although the collection relationship is apparent, an example where it does not hold is generation compaction. When a generation is collected it may also be compacted. The LOH is not however compacted, so it cannot be said that everything that happens to generation 2 objects happens to the objects in the LOH.

[Test]
public void large_object_heap_objects_are_reported_as_max_generation()
{
    int[] bling = new int[85000 / 4];

    int maxGen = GC.MaxGeneration;
    int objectGen = GC.GetGeneration(bling);

    Assert.AreEqual(maxGen, objectGen, "Large object is at max generation.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文