垃圾回收如何决定变量的生成 [英] How garbage collection decides generation for variable

查看:49
本文介绍了垃圾回收如何决定变量的生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道GC有3(0,1,2)代,但是我想知道GC如何决定变量的生成?

I know GC has 3 (0, 1, 2) generations but I'm wondering how GC decides the generation for a variable?

我认为所有变量都进入了第0代,并在一段时间后移至第1代和第2代.GC决定这一代的大小是否重要?

I thought all variables go into generation 0 and after some time move to generations 1 and 2. Is it size matter for GC to decide the generation?

程序1:

private static void Main(string[] args)
{
        int a = 0;
        string name = "Test";
        var byteArray = new byte[10];
        byteArray[4] = 4;

        Console.WriteLine($"Generation of array {GC.GetGeneration(byteArray)}");
        Console.WriteLine($"Generation of local int variable {GC.GetGeneration(a)}");
        Console.WriteLine($"Generation of local string variable {GC.GetGeneration(name)}");
}

结果

Generation of array 0
Generation of local int variable 0
Generation of local string variable 0

程序2:

private static void Main(string[] args)
{
        int a = 0;
        string name = "Test";
        var byteArray = new byte[100000000];
        byteArray[4] = 4;

        Console.WriteLine($"Generation of array {GC.GetGeneration(byteArray)}");
        Console.WriteLine($"Generation of local int variable {GC.GetGeneration(a)}");
        Console.WriteLine($"Generation of local string variable {GC.GetGeneration(name)}");

}

结果

Generation of array 2
Generation of local int variable 0
Generation of local string variable 0

推荐答案

我想知道GC如何决定变量的生成?

I'm wondering how GC decides the generation for a variable?

首先不对变量进行GC处理.引用类型的对象已进行GC处理.这些对象包含变量,但是收集的是 object .

Variables are not GC'd in the first place. Objects of reference type are GC'd. Those objects contain variables, but it is the object that is collected.

我认为所有变量都进入了第0代,并在一段时间后移到了第1代和第2代.

I thought all variables go into generation 0 and after some time move to generations 1 and 2.

不.不要以变量的方式思考.考虑对象.新分配的对象进入gen0.如果该对象在集合中幸存下来,则将其移动到gen1.如果它在另一个集合中幸存下来,则移至gen2.

No. Don't think in terms of variables. Think in terms of objects. A newly-allocated object goes in gen0. If that object survives a collection, it is moved to gen1. If it survives another collection, it moves to gen2.

所以现在您知道了为什么在包含int的局部变量上调用 GetGeneration 总是返回零.这不是因为本地以某种方式与gen0神奇地相关联.本地不是引用类型的对象,其值也不是!

So now you know why calling GetGeneration on a local variable containing an int will always return zero. It is not because the local is somehow magically associated with gen0. The local is not an object of reference type, and neither is its value!

当您将int传递给 GetGeneration 时,该int将被装箱到一个对象中;该对象是新分配的,因此它是第0代.这不会告诉您有关变量的任何信息.如果将 any (非空)值类型传递给 GetGeneration ,这会触发分配,并且显然分配是gen0.

When you pass the int to GetGeneration the int is boxed into an object; that object is newly allocated and so it is in generation 0. This tells you nothing about the variable. If you pass any (non-null) value type to GetGeneration that triggers an allocation, and obviously that allocation is gen0.

同样,您的问题表明您认为GC在考虑变量的生命周期.不是.它正在查看引用类型的对象的生存期.

Again, your question indicates that you think that the GC is looking at the lifetimes of variables. It is not. It is looking at the lifetimes of objects of reference type.

GC决定世代大小是否重要?

Is it size matter for GC to decide the generation?

是的.例如,从特殊的大对象堆"中分配极大的数组.极大"是指基本上等于或大于85KB的任何对象.大对象堆(1)不压缩,并且(2)仅在常规堆进行gen2收集时收集.

Yes. Extremely large arrays, for example, are allocated from a special "large object heap". By "extremely large", we mean basically any object on the order of 85KB or bigger. The large object heap is (1) not compacted, and (2) only collected when the regular heap does a gen2 collection.

这篇关于垃圾回收如何决定变量的生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆