同步运行 GC.Collect

Run GC.Collect synchronously(同步运行 GC.Collect)
本文介绍了同步运行 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# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)