Does garbage collection run during debug?

前端 未结 2 1589
-上瘾入骨i
-上瘾入骨i 2020-11-29 10:49

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 witho

2条回答
  •  离开以前
    2020-11-29 11:09

    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.

提交回复
热议问题