Alex Rivera | Logout

Is GC.Collect() blocking?

Asked 2011-06-18T22:43:27.507
16

I'm running some benchmark tests over my code and I want to make sure a garbage collect doesn't occur during one of my benchmarks because it's cleaning up the mess of a prior test. I figure my best chance of this is to force a collect before starting a benchmark.

So I'm calling GC.Collect() before a benchmark starts but not sure if a collect continues to run in a separate thread, etc and returns immediately. If it does run on a BG thread I want to know how to call it synchronously or a at least wait til it's finished the collect.

Edit
Report

1 Answer

2

If you want to benchmark your code, it should be done over a course of many iterations and as an average. You are never guaranteed when GC is run in the first place.

answered 2011-06-18T22:45:36.420

Your Answer