Tes*_*ess 10 .net c# garbage-collection
鉴于以下内容:
GC.Collect(GC.MaxGeneration);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration);
Run Code Online (Sandbox Code Playgroud)
考虑到多线程和垃圾收集模式,在什么情况下你会遇到死锁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)
{
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6219 次 |
| 最近记录: |