Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run GC.Collect synchronously

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.)

like image 765
Joe White Avatar asked Apr 14 '09 18:04

Joe White


People also ask

Can we call GC collect?

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.

What does GC collect () do?

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.

Can we call GC collect method and when to call?

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.

When you request to GC to run it will start to run immediately True or false?

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.


1 Answers

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.

like image 169
Brian Rasmussen Avatar answered Sep 28 '22 07:09

Brian Rasmussen