[英]GC.GetGeneration() returns always 0 for int variable even after calling GC.Collect() in C#
Below is code snippet in which I am getting generation of variable int j
before and after calling garbage collector.下面是代码片段,其中我在调用垃圾收集器之前和之后生成变量
int j
。
int j = 30;
WriteLine(GC.GetGeneration(j) );
GC.Collect();
WriteLine(GC.GetGeneration(j));
The output should be output 应该是
0
1
as int j
is surviving garbage collection But I am getting因为
int j
在垃圾收集中幸存下来但我得到了
0
0
I don't understand why this is happening because int
is also an object in C#.我不明白为什么会这样,因为
int
也是 C# 中的 object。 PS: I have tried running project in debug as well as in release mode. PS:我尝试过在调试和发布模式下运行项目。
As mentioned in the comments.如评论中所述。 an
int
is a value type and is not tracked by the garbage collector. int
是一种值类型,不被垃圾收集器跟踪。 Since GetGeneration
takes an object
, the int will be boxed .由于
GetGeneration
采用object
,因此 int 将被装箱。 Ie a new object will be created.即将创建一个新的 object。 That new object will always be allocated in gen 0. The next time you call
GetGeneration
the same thing will occur.新的 object 将始终在 gen 0 中分配。下次调用
GetGeneration
时会发生同样的事情。
So your results are as expected.所以你的结果符合预期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.