• <i id='H1jSh'><tr id='H1jSh'><dt id='H1jSh'><q id='H1jSh'><span id='H1jSh'><b id='H1jSh'><form id='H1jSh'><ins id='H1jSh'></ins><ul id='H1jSh'></ul><sub id='H1jSh'></sub></form><legend id='H1jSh'></legend><bdo id='H1jSh'><pre id='H1jSh'><center id='H1jSh'></center></pre></bdo></b><th id='H1jSh'></th></span></q></dt></tr></i><div id='H1jSh'><tfoot id='H1jSh'></tfoot><dl id='H1jSh'><fieldset id='H1jSh'></fieldset></dl></div>
    <legend id='H1jSh'><style id='H1jSh'><dir id='H1jSh'><q id='H1jSh'></q></dir></style></legend>

      <bdo id='H1jSh'></bdo><ul id='H1jSh'></ul>

    <small id='H1jSh'></small><noframes id='H1jSh'>

      1. <tfoot id='H1jSh'></tfoot>

        同步运行 GC.Collect

        时间:2023-09-13

          <tbody id='Rjzqc'></tbody>

          • <i id='Rjzqc'><tr id='Rjzqc'><dt id='Rjzqc'><q id='Rjzqc'><span id='Rjzqc'><b id='Rjzqc'><form id='Rjzqc'><ins id='Rjzqc'></ins><ul id='Rjzqc'></ul><sub id='Rjzqc'></sub></form><legend id='Rjzqc'></legend><bdo id='Rjzqc'><pre id='Rjzqc'><center id='Rjzqc'></center></pre></bdo></b><th id='Rjzqc'></th></span></q></dt></tr></i><div id='Rjzqc'><tfoot id='Rjzqc'></tfoot><dl id='Rjzqc'><fieldset id='Rjzqc'></fieldset></dl></div>
          • <tfoot id='Rjzqc'></tfoot>
          • <small id='Rjzqc'></small><noframes id='Rjzqc'>

            <legend id='Rjzqc'><style id='Rjzqc'><dir id='Rjzqc'><q id='Rjzqc'></q></dir></style></legend>
                • <bdo id='Rjzqc'></bdo><ul id='Rjzqc'></ul>
                  本文介绍了同步运行 GC.Collect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是什么阻止了 C# 中的线程被收集? 下一篇:什么时候对象会被垃圾回收?

                  相关文章

                  <i id='jdIVk'><tr id='jdIVk'><dt id='jdIVk'><q id='jdIVk'><span id='jdIVk'><b id='jdIVk'><form id='jdIVk'><ins id='jdIVk'></ins><ul id='jdIVk'></ul><sub id='jdIVk'></sub></form><legend id='jdIVk'></legend><bdo id='jdIVk'><pre id='jdIVk'><center id='jdIVk'></center></pre></bdo></b><th id='jdIVk'></th></span></q></dt></tr></i><div id='jdIVk'><tfoot id='jdIVk'></tfoot><dl id='jdIVk'><fieldset id='jdIVk'></fieldset></dl></div>

                    <bdo id='jdIVk'></bdo><ul id='jdIVk'></ul>

                      <legend id='jdIVk'><style id='jdIVk'><dir id='jdIVk'><q id='jdIVk'></q></dir></style></legend>

                      <small id='jdIVk'></small><noframes id='jdIVk'>

                    1. <tfoot id='jdIVk'></tfoot>