Does the .NET garbage collector perform predictive analysis of code?

后端 未结 4 1400
不思量自难忘°
不思量自难忘° 2020-12-14 06:48

OK, I realize that question might seem weird, but I just noticed something that really puzzled me... Have a look at this code :

static void TestGC()
{
               


        
4条回答
  •  遥遥无期
    2020-12-14 07:15

    The garbage collector gets lifetime hints from the JIT compiler. It thus knows that at the GC.Collect() call there are no more possible references to the local variables and that they therefore can be collected. Review GC.KeepAlive()

    When a debugger is attached, JIT optimization is disabled and the lifetime hint gets extended to the end of the method. Makes debugging a lot simpler.

    I wrote a much more detailed answer about this, you'll find it here.

提交回复
热议问题