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?
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.
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.)
To have the garbage collector consider all objects regardless of their generation, use the version of this method that takes no parameters. To have the garbage collector reclaim objects based on a GCCollectionMode setting, use the GC. Collect(Int32, GCCollectionMode) method overload.
It performs a blocking garbage collection of all generations. All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.
Collect() method is made. Rather, such objects would be placed in the finalization queue. If you would like to collect those objects as well, you would need to make a call to the GC. WaitForPendingFinalizers() method so that those objects are cleaned up when the next GC cycle runs.
2 Answers. Show activity on this post. Calling GC. Collect() will do a complete garbage collection and wait for it to finish, but it will NOT wait for any pending finalizers to run.
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();
The Collect()
will schedule any non-rooted instances for finalization and then the thread will wait for the finalizer thread to complete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With