鉴于以下情况:
GC.Collect(GC.MaxGeneration);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration);
考虑到多线程和垃圾收集模式,什么情况下会出现死锁WaitForPendingFinalizers
?
注意:我并不是在寻找有关您不应该打电话的原因的答案GC.Collect
.
// causes a deadlock when built with release config and no debugger attached
// building in debug mode and/or attaching the debugger might keep
// badIdea alive for longer, in which case you won't see the deadlock
// unless you explicitly set badIdea to null after calling Monitor.Enter
var badIdea = new BadIdea();
Monitor.Enter(badIdea);
GC.Collect(GC.MaxGeneration);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration);
// ...
public class BadIdea
{
~BadIdea()
{
lock (this)
{
// ...
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)