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

后端 未结 4 1385
不思量自难忘°
不思量自难忘° 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:02

    Could someone shed some light on what's going on?

    Your mental model of how the virtual machine garbage collects resources is unrealistically simple. In particular, your assumption that variables and scope are somehow relevant to garbage collection is wrong.

    Is this related to JIT optimizations?

    Yes.

    What's happening exactly?

    The JIT didn't bother keeping references around unnecessarily so the tracing collector didn't find references when it kicked it so it collected the objects.

    Note that other answers have stated that the JIT conveyed this information to the GC when the truth is actually that the JIT did not convey those references to the GC because there would be no point in doing so.

提交回复
热议问题