[英]Why is destructor never called?
From what I understand the destructor is called when a class instance goes out of scope and the garbage collector makes a pass.据我了解,当类实例超出范围并且垃圾收集器通过时,将调用析构函数。 In the code below the destructor is never called.
在下面的代码中,永远不会调用析构函数。 What's going on?
这是怎么回事?
while (true)
{
var Thing1 = new Thing();
await Task.Delay(100);
}
class Thing
{
~Thing()
{
Console.WriteLine("Destructed");
}
}
With this code I managed to call the Finalizer
.使用此代码,我设法调用了
Finalizer
。 I guess, the garbage collector needs the heap to fill up to a certain point before it makes a pass.我猜,垃圾收集器需要堆填满到某个点才能通过。
while (true)
{
var Thing1 = new Thing();
await Task.Delay(100);
}
class Thing
{
public List<int> List { get; } = new(1000000);
~Thing()
{
Console.WriteLine("Destructed");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.