Difference between 3rd gen objects and large object heap

只谈情不闲聊 提交于 2020-01-02 06:25:17

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/4875364/difference-between-3rd-gen-objects-and-large-object-heap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!