简体   繁体   English

第3代对象和大对象堆之间的区别

[英]Difference between 3rd gen objects and large object heap

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

The LOH (Large Object Heap) is a single heap where large objects are allocated directly and stay there until they are collected. LOH(大对象堆)是一个单独的堆,其中大对象直接分配并保持在那里直到它们被收集。 Objects are directly allocated into the LOH based on their size eg being equal or greater than 85000 bytes. 对象基于它们的大小直接分配到LOH中,例如等于或大于85000字节。

Generational objects are "small" objects that are allocated into the SOH (Small Object Heap) which is a single heap. 分代对象是分配到SOH(小对象堆)中的“小”对象,SOH是单个堆。 Objects in the SOH have an associated generation which denotes how many collections they have survived up to the maximum generation eg 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 ie generations 0,1,2. SOH中的对象具有相关联的生成,其表示它们在最大生成期间存活了多少集合,例如2.当生成数从0开始时,第2代中的对象可以被描述为第3代,因为它已经存在于最小值3个集合,即代0,1,2。

Generations helps to optimize garbage scanning. Generations有助于优化垃圾扫描。 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. 生成方案应用于SOH,因为它被视为对存在大量对象的堆的良好优化。

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. 据我所知,LOH对象被报告为最大代,但我相信这只是一个默认值。 They are not actually in any generation ie generation 2 SOH objects and LOH objects are not in the same "list". 它们实际上并不在任何一代中,即第2代SOH对象和LOH对象不在同一“列表”中。 However, as pointed out by @Henk, when performing a generation 2 collection, LOH objects are also collected at this time. 但是,正如@Henk所指出的那样,在执行第2代集合时,此时也会收集LOH对象。 So conceptually there is a relationship between generation 2 and the LOH. 因此,概念上,第2代和LOH之间存在关系。 This is correct as of .Net 2.0: 从.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. 从世代的角度来看,大型对象属于第2代,因为只有在第2代集合时才会收集它们。

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. 然而,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.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM