I have a program that opens an Excel COM object, does some stuff, and closes it. Then I want to move that file after it's closed. This works fine if I run the program without break points. But, if I step into something in debug mode before I attempt to move the file I get an IOException: "The process cannot access the file because it is being used by another process."
So what's the deal? Is garbage collection performing better while a program is allowed to run at full speed as opposed to while I am stepping through it? Is stepping through my code doing more than just very slowly running it? Are there other consequences to the debug mode? Other errors that are encountered simply because I am in debug and not running an exe?
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.