问题描述
GC.Collect 似乎在后台线程中启动垃圾收集,然后立即返回.如何同步运行 GC.Collect - 即等待垃圾收集完成?
GC.Collect appears to start the garbage collection in a background thread, and then return immediately. How can I run GC.Collect synchronously -- i.e., wait for the garbage collection to complete?
这是在 NUnit 测试的上下文中.我尝试将 gcConcurrent 设置添加到我的测试程序集的 app.config 文件中,我对 nunit.exe.config 进行了同样的尝试.两者都没有任何效果——当我调试时,我仍然可以看到终结器在GC终结器线程"上运行,而不是调用 GC.Collect 的线程(NUnit 的TestRunnerThread"),并且两个线程同时运行.
This is in the context of NUnit tests. I tried adding the gcConcurrent setting to my test assembly's app.config file, and I tried the same with nunit.exe.config. Neither had any effect -- when I debug, I can still see the finalizer being run on the "GC Finalizer Thread", rather than the thread that called GC.Collect (NUnit's "TestRunnerThread"), and both threads are running concurrently.
背景:我希望我的测试在泄漏(不要调用 Dispose on)特定类时失败.所以我在那个类中添加了一个终结器,它设置了一个静态 wasLeaked 标志;然后我的测试 TearDown 调用 GC.Collect() 然后如果 wasLeaked 为真则抛出.但它并没有确定性地失败,因为当它读取 wasLeaked 时,通常甚至还没有调用终结器.(在垃圾收集最终完成后,它会失败一些稍后的测试.)
Background: I want my tests to fail if they leak (don't call Dispose on) a particular class. So I've added a finalizer to that class that sets a static wasLeaked flag; then my test TearDown calls GC.Collect() and then throws if wasLeaked is true. But it's not failing deterministically, because when it reads wasLeaked, the finalizer usually hasn't even been called yet. (It fails some later test instead, after the garbage collection finally finishes.)
推荐答案
终结器在专用的高优先级后台线程上运行.从你帖子的背景来看,我猜你可以简单地做
Finalizers are run on a dedicated, high-priority background thread. From the background in your post, I gather that you can simply do
GC.Collect();
GC.WaitForPendingFinalizers();
Collect() 将调度任何非根实例进行终结,然后线程将等待终结器线程完成.
The Collect() will schedule any non-rooted instances for finalization and then the thread will wait for the finalizer thread to complete.
这篇关于同步运行 GC.Collect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)