c# – 垃圾收集器是否受益于对Collect和WaitForPendingFinalizer
我发现这个在线代码是在取消初始化Excel Interop对象后附加的:
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); 这是对DRY的准违反(以口吃的方式连续两次调用GC.Collect()和GC.WaitForPendingFinalizers())有什么用处,或者只是浪费时间? 解决方法
这来自Microsoft
documentation站点上的示例程序.
//Force garbage collection. GC.Collect(); // Wait for all finalizers to complete before continuing. // Without this call to GC.WaitForPendingFinalizers,// the worker loop below might execute at the same time // as the finalizers. // With this call,the worker loop executes only after // all finalizers have been called. GC.WaitForPendingFinalizers();
据我所知,没有明确的迹象表明两次调用它的好处. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |