Run GC.Collect synchronously(同步运行 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:同步运行 GC.Collect
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01