Does garbage collection run during debug?

2019-01-06 22:53发布

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?

1条回答
手持菜刀,她持情操
2楼-- · 2019-01-06 23:12

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.

查看更多
登录 后发表回答