c# – 垃圾收集器是否受益于对Collect和WaitForPendingFinalizers()的多次调用?

c# – 垃圾收集器是否受益于对Collect和WaitForPendingFinalizers()的多次调用?,第1张

概述我发现这个在线代码是在取消初始化Excel Interop对象后附加的: GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers(); 这是对DRY的准违反(以口吃的方式连续两次调用GC.Collect()和GC.WaitForPendingFinalizers())有什么用处,或 我发现这个在线代码是在取消初始化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();

When the garbage collector finds objects that can be reclaimed,it checks each object to determine the object’s finalization requirements. If an object implements a finalizer and has not Disabled finalization by calling SuppressFinalize,the object is placed in a List of objects that are marked as ready for finalization. The garbage collector calls the Finalize methods for the objects in this List and removes the entrIEs from the List. This method blocks until all finalizers have run to completion.

The thread on which finalizers are run is unspecifIEd,so there is no guarantee that this method will terminate. However,this thread can be interrupted by another thread while the WaitForPendingFinalizers method is in progress. For example,you can start another thread that waits for a period of time and then interrupts this thread if this thread is still suspended.

据我所知,没有明确的迹象表明两次调用它的好处.

总结

以上是内存溢出为你收集整理的c# – 垃圾收集器是否受益于对Collect和WaitForPendingFinalizers()的多次调用?全部内容,希望文章能够帮你解决c# – 垃圾收集器是否受益于对Collect和WaitForPendingFinalizers()的多次调用?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1220112.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存