Does garbage collection run during debug?

Garbage collection is optimized differently when running not in the debugger, yes. In particular, the CLR can detect that a variable won't be used for the rest of a method, and treat it as not a GC root any more. In the debugger, variables in scope act as GC roots throughout the method so that you can still examine the values with the debugger.

However, that should rarely be a problem - it should only affect things if a finalizer actually performs some clean-up, and if you're explicitly tidying things up in a timely way (e.g. with using statements) you usually wouldn't notice the difference.


For the record, I ran into this as well a few times. I found that this works when testing finalizers calling native side code in debug mode:

((Action)()=>{
   // Do your stuff in here ...
})();

GC.Collect();
GC.WaitForPendingFinalizers();

The garbage collector seems to keep a copy of allocations rooted within the local method scope, so by creating a new method scope and exiting, the GC usually releases the resource. So far this works well for my debugging purposes.