CLR via C# 读书笔记 5-6 垃圾回收的Generation

直接用代码和注释说明问题吧~~

 

代码
   
   
public static class Program
{
internal sealed class GenObj
{
~ GenObj()
{
Console.WriteLine(
" In Finalize method " );
}
}
public static void Main()
{
Console.WriteLine(
" Maximum generations: " + GC.MaxGeneration);
// Create a new GenObj in the heap.
Object o = new GenObj();
// Because this object is newly created, it is in generation 0.
Console.WriteLine( " Gen " + GC.GetGeneration(o)); // 0 这个方法是用来获取对象在第几代

// Performing a garbage collection promotes the object's generation.
GC.Collect();
Console.WriteLine(
" Gen " + GC.GetGeneration(o)); // 1 每次GC以后还存活的单位就升级
GC.Collect();
Console.WriteLine(
" Gen " + GC.GetGeneration(o)); // 2 每次GC以后还存活的单位就升级
GC.Collect();
Console.WriteLine(
" Gen " + GC.GetGeneration(o)); // 2 (max) 每次GC以后还存活的单位就升级 最高只有2级 - -#

o
= null ; // 移除强引用关系
Console.WriteLine( " Collecting Gen 0 " );
GC.Collect(
0 ); // Collect generation 0. 回收指定代的内存
GC.WaitForPendingFinalizers(); // Finalize is NOT called.
Console.WriteLine( " Collecting Gens 0, and 1 " );
GC.Collect(
1 ); // Collect generations 0 & 1.
GC.WaitForPendingFinalizers(); // Finalize is NOT called.
Console.WriteLine( " Collecting Gens 0, 1, and 2 " );
GC.Collect(
2 ); // Same as Collect()
GC.WaitForPendingFinalizers(); // Finalize IS called. 这里这个对象才被彻底做掉了....
}

}

 

第一次感觉一个对象的Finalize要等这么久....

当然,如果在0代对象的时候就被回收了 就没这么麻烦了

- -#

 

 

PS:GC的简单逻辑:

  1.遍历Generation 0  没用的拉出去杀掉,还有用的升级到Generation 1 ,

  2.如果空间已经过了, 就不继续回收了 ,返回

  3.如果空间还不够或者Generation 1 也满了, 把G1没用的拉出去烧了祭天,然后还活着的升级到G2

  4.如果有必要 继续回收G2...如果g2也不够了 那么就OutOfMemoryException ....

  

参数:

GC在初始化完成的时候Generation 0 的大小就确定为256k (优先考虑放在L2 Cache中)

Generation 1 开始大小是0  最大可以到2M

Generation 2  没有限制

你可能感兴趣的:(垃圾回收)