.NET 垃圾收集器是否执行代码预测分析?

发布于 2024-09-07 12:22:31 字数 1041 浏览 4 评论 0原文

好吧,我意识到这个问题可能看起来很奇怪,但我刚刚注意到一些真正让我困惑的事情......看看这段代码:

static void TestGC()
{
        object o1 = new Object();
        object o2 = new Object();
        WeakReference w1 = new WeakReference(o1);
        WeakReference w2 = new WeakReference(o2);

        GC.Collect();

        Console.WriteLine("o1 is alive: {0}", w1.IsAlive);
        Console.WriteLine("o2 is alive: {0}", w2.IsAlive);
}

因为 o1o2 仍然在当垃圾收集发生时,我预计会出现以下输出:

o1 还活着:正确
o2 还活着:正确

,但是,这就是我得到的:

o1 还活着:假
o2 还活着:假

注意:仅当代码在发布模式下编译并在调试器外部运行时才会发生这种情况

我的猜测是 GC 检测到 o1< /code> 和 o2 在超出范围之前不会再次使用,并尽早收集它们。为了验证这个假设,我在 TestGC 方法的末尾添加了以下行:

string s = o2.ToString();

我得到了以下输出:

o1 还活着:假
o2 还活着:正确

因此在这种情况下,不会收集 o2

有人可以阐明正在发生的事情吗?这与 JIT 优化有关吗?究竟发生了什么?

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()
{
        object o1 = new Object();
        object o2 = new Object();
        WeakReference w1 = new WeakReference(o1);
        WeakReference w2 = new WeakReference(o2);

        GC.Collect();

        Console.WriteLine("o1 is alive: {0}", w1.IsAlive);
        Console.WriteLine("o2 is alive: {0}", w2.IsAlive);
}

Since o1 and o2 are still in scope when the garbage collection occurs, I would have expected the following output:

o1 is alive: True
o2 is alive: True

But instead, here's what I got:

o1 is alive: False
o2 is alive: False

NOTE : this occurs only when the code is compiled in Release mode and run outside the debugger

My guess is that the GC detects that o1 and o2 won't be used again before they go out of scope, and collects them early. To validate this hypothesis, I added the following line at the end of the TestGC method :

string s = o2.ToString();

And I got the following output :

o1 is alive: False
o2 is alive: True

So in that case, o2 isn't collected.

Could someone shed some light on what's going on ? Is this related to JIT optimizations ? What's happening exactly ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离线来电— 2024-09-14 12:22:31

垃圾收集器依赖于 JIT 编译器提供的编译到程序集中的信息,该信息告诉它仍在使用各种变量和“事物”的代码地址范围。

因此,在您的代码中,由于您不再使用对象变量,GC 可以自由地收集它们。 WeakReference 不会阻止这种情况,事实上,这就是 WR 的全部要点,允许您保留对对象的引用,同时不阻止它被收集。

关于 WeakReference 对象的案例很好地总结为: MSDN 上的行描述:

表示弱引用,它引用一个对象,同时仍然允许垃圾收集回收该对象。

WeakReference 对象不会被垃圾收集,因此您可以安全地使用它们,但它们引用的对象只剩下 WR 引用,因此可以自由收集。

通过调试器执行代码时,变量的作用域会被人为地扩展,直到其作用域结束,通常是声明它们的块的末尾(如方法),以便您可以在断点处检查它们。

有一些微妙的事情需要发现。考虑以下代码:

using System;

namespace ConsoleApplication20
{
    public class Test
    {
        public int Value;

        ~Test()
        {
            Console.Out.WriteLine("Test collected");
        }

        public void Execute()
        {
            Console.Out.WriteLine("The value of Value: " + Value);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Console.Out.WriteLine("Leaving Test.Execute");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test { Value = 15 };
            t.Execute();
        }
    }
}

在发布模式下,在没有附加调试器的情况下执行,输出如下:

The value of Value: 15
Test collected
Leaving Test.Execute

这样做的原因是,即使您仍在与 Test 对象关联的方法内执行,在要求 GC 执行操作时事情就是这样,不需要任何对 Test 的实例引用(不需要引用 thisValue),也不需要调用任何要执行的实例方法,因此对象可以安全地收集。

如果您没有意识到,这可能会产生一些令人讨厌的副作用。

考虑以下类:

public class ClassThatHoldsUnmanagedResource : IDisposable
{
    private IntPtr _HandleToSomethingUnmanaged;

    public ClassThatHoldsUnmanagedResource()
    {
        _HandleToSomethingUnmanaged = (... open file, whatever);
    }

    ~ClassThatHoldsUnmanagedResource()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        (release unmanaged resource here);
        ... rest of dispose
    }

    public void Test()
    {
        IntPtr local = _HandleToSomethingUnmanaged;

        // DANGER!

        ... access resource through local here
    }

此时,如果 Test 在获取非托管句柄的副本后不使用任何实例数据怎么办?如果 GC 现在运行在我写“DANGER”的地方怎么办?你知道这是怎么回事吗?当 GC 运行时,它将执行终结器,该终结器将从仍在执行的 Test 下撤回对非托管资源的访问。

非托管资源(通常通过 IntPtr 或类似资源访问)对于垃圾收集器来说是不透明的,并且在判断对象的生命周期时不会考虑这些资源。

换句话说,我们在局部变量中保留对句柄的引用对于 GC 来说是没有意义的,它只会注意到没有剩下实例引用,因此认为该对象可以安全地收集。

这当然假设没有外部引用仍然被认为是“活动的”对象。例如,如果上面的类是从这样的方法中使用的:

public void DoSomething()
{
    ClassThatHoldsUnmanagedResource = new ClassThatHoldsUnmanagedResource();
    ClassThatHoldsUnmanagedResource.Test();
}

那么您会遇到完全相同的问题。

(当然,您可能不应该这样使用它,因为它实现了 IDisposable,您应该使用 using 块或手动调用 Dispose。 )

编写上述方法的正确方法是强制 GC 在我们仍然需要对象时不会收集它:

public void Test()
{
    IntPtr local = _HandleToSomethingUnmanaged;

    ... access resource through local here

    GC.KeepAlive(this); // won't be collected before this has executed
}

The Garbage Collector relies on information compiled into your assembly provided by the JIT compiler that tells it what code address ranges various variables and "things" are still in use over.

As such, in your code, since you no longer use the object variables GC is free to collect them. WeakReference will not prevent this, in fact, this is the whole point of a WR, to allow you to keep a reference to an object, while not preventing it from being collected.

The case about WeakReference objects is nicely summed up in the one-line description on MSDN:

Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection.

The WeakReference objects are not garbage collected, so you can safely use those, but the objects they refer to had only the WR reference left, and thus were free to collect.

When executing code through the debugger, variables are artificially extended in scope to last until their scope ends, typically the end of the block they're declared in (like methods), so that you can inspect them at a breakpoint.

There's some subtle things to discover with this. Consider the following code:

using System;

namespace ConsoleApplication20
{
    public class Test
    {
        public int Value;

        ~Test()
        {
            Console.Out.WriteLine("Test collected");
        }

        public void Execute()
        {
            Console.Out.WriteLine("The value of Value: " + Value);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Console.Out.WriteLine("Leaving Test.Execute");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test { Value = 15 };
            t.Execute();
        }
    }
}

In Release-mode, executed without a debugger attached, here's the output:

The value of Value: 15
Test collected
Leaving Test.Execute

The reason for this is that even though you're still executing inside a method associated with the Test object, at the point of asking GC to do it's thing, there is no need for any instance references to Test (no reference to this or Value), and no calls to any instance-method left to perform, so the object is safe to collect.

This can have some nasty side-effects if you're not aware of it.

Consider the following class:

public class ClassThatHoldsUnmanagedResource : IDisposable
{
    private IntPtr _HandleToSomethingUnmanaged;

    public ClassThatHoldsUnmanagedResource()
    {
        _HandleToSomethingUnmanaged = (... open file, whatever);
    }

    ~ClassThatHoldsUnmanagedResource()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        (release unmanaged resource here);
        ... rest of dispose
    }

    public void Test()
    {
        IntPtr local = _HandleToSomethingUnmanaged;

        // DANGER!

        ... access resource through local here
    }

At this point, what if Test doesn't use any instance-data after grabbing a copy of the unmanaged handle? What if GC now runs at the point where I wrote "DANGER"? Do you see where this is going? When GC runs, it will execute the finalizer, which will yank the access to the unmanaged resource out from under Test, which is still executing.

Unmanaged resources, typically accessed through an IntPtr or similar, is opaque to the garbage collector, and it does not consider these when judging the life of an object.

In other words, that we keep a reference to the handle in a local variable is meaningless to GC, it only notices that there are no instance-references left, and thus considers the object safe to collect.

This if course assumes that there is no outside reference to the object that is still considered "alive". For instance, if the above class was used from a method like this:

public void DoSomething()
{
    ClassThatHoldsUnmanagedResource = new ClassThatHoldsUnmanagedResource();
    ClassThatHoldsUnmanagedResource.Test();
}

Then you have the exact same problem.

(of course, you probably shouldn't be using it like this, since it implements IDisposable, you should be using a using-block or calling Dispose manually.)

The correct way to write the above method is to enforce that GC won't collect our object while we still need it:

public void Test()
{
    IntPtr local = _HandleToSomethingUnmanaged;

    ... access resource through local here

    GC.KeepAlive(this); // won't be collected before this has executed
}
电影里的梦 2024-09-14 12:22:31

垃圾收集器从 JIT 编译器获取生命周期提示。因此,它知道在 GC.Collect() 调用时不再可能引用局部变量,因此可以收集它们。查看 GC.KeepAlive()

当附加调试器时,JIT 优化将被禁用,并且生命周期提示将扩展到方法的末尾。使调试变得更加简单。

我对此写了一个更详细的答案,您可以在这里找到

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.

长伴 2024-09-14 12:22:31

我不是 C# 专家,但我想说这是因为在生产中您的代码已优化,

static void TestGC()
{
        WeakReference w1 = new WeakReference(new Object());
        WeakReference w2 = new WeakReference(new Object());

        GC.Collect();

        Console.WriteLine("o1 is alive: {0}", w1.IsAlive);
        Console.WriteLine("o2 is alive: {0}", w2.IsAlive);
}

不再保留 o1、o2 绑定。

编辑:这是一个名为常量折叠的编译器优化。
无论有或没有 JIT 都可以完成。

如果您有办法禁用 JIT 但保留发布优化,您将会有相同的行为。

人们应该注意以下注意事项:

注意:只有当代码
以Release模式编译并运行
在调试器之外

这是关键。

PS:我还假设您了解 WeakReference 的含义。

I am not an C# expert but I would say that this is because in production your code is optimize in

static void TestGC()
{
        WeakReference w1 = new WeakReference(new Object());
        WeakReference w2 = new WeakReference(new Object());

        GC.Collect();

        Console.WriteLine("o1 is alive: {0}", w1.IsAlive);
        Console.WriteLine("o2 is alive: {0}", w2.IsAlive);
}

No more o1, o2 binding remain.

EDIT: This is a Compiler optimization named constant folding.
Which can be done with or without a JIT.

If you have a way to disable the JIT but kept the release optimization you're gonna have the same behavior.

People should pay attention to the note:

NOTE : this occurs only when the code
is compiled in Release mode and run
outside the debugger

this is the key.

PS: I am also assuming that your understand what WeakReference mean.

怪我鬧 2024-09-14 12:22:31

有人可以解释一下发生了什么吗?

您对虚拟机垃圾如何收集资源的思维模型过于简单。特别是,您认为变量和范围与垃圾收集在某种程度上相关的假设是错误的。

这与 JIT 优化有关吗?

是的。

到底发生了什么?

JIT 不会费心保留不必要的引用,因此跟踪收集器在启动时没有找到引用,因此它收集了对象。

请注意,其他答案指出 JIT 将此信息传递给 GC,而事实是 JIT 没有将这些引用传递给 GC,因为这样做是没有意义的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文