namespace GcDemo
{
class Program
{
static void Main(string[] args)
{
var list = new List<object>();
Console.WriteLine("list is in {0} generation.", GC.GetGeneration(list));
GC.Collect();
Console.WriteLine("list is in {0} generation.", GC.GetGeneration(list));
GC.Collect();
list.Add(new object());
Console.WriteLine("list is in {0} generation. object is in {1} generation.", GC.GetGeneration(list), GC.GetGeneration(list[0]));
GC.Collect(0);
Console.WriteLine("list is in {0} generation. object is in {1} generation.", GC.GetGeneration(list), GC.GetGeneration(list[0]));
}
}
}
列表對象在第2代,而這是唯一的參考列表[0]對象,它是在根0爲什麼它知道不收集它在GC.Collect(0)?
您是否期望收集'object'實例? –
@BrianRasmussen是的。 –
爲什麼會這樣?它仍然紮根,因此不適合收藏。如果gen0對象不能可靠地被gen1或2中的對象所紮根,那麼整個世代的想法就毫無意義。 –