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.

Leave a Comment