正子对象还活着的时候Object.Finalize被称为GC? [英] Are child objects still alive when Object.Finalize is called by GC?

查看:169
本文介绍了正子对象还活着的时候Object.Finalize被称为GC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说,我有这个类:

class Test
{
    readonly object _child = new Object();

    // ...

    ~Test()
    {
        // access _child here
        // ...
    }
}

_child 对象的质量保证的自己还活着的时候〜测试被称为由垃圾收集?或者我应该先针的 _child GCHandle.Alloc 在构造函数?

Is the _child object guaranteed to still be alive when ~Test is called by the garbage collector? Or should I first "pin" the _child with GCHandle.Alloc in the constructor?

推荐答案

作为一个只读字段 _child 即可'T失去其参考(除非你通过反射将其设置为空)。这意味着,直到测试是垃圾回收 _child 将保持在内存中是肯定的。

Being a readonly field _child you can't lose its reference (unless you set it to null via reflection). Which means that till the Test is garbage collected _child will stay in memory for sure.

此外,您使用的是终结这是垃圾回收前呼吁,只有在接下来的传递对象的内存会被回收,在这一点上 _child 一定会健在。在当的Finalize 方法被调用换句话说 _child 将活着,安全地访问它。

Also, you're using a Finalizer which is called prior to garbage collection, Only on next pass the object's memory will be reclaimed, at this point _child will be alive. In other words when Finalize method is called _child will be alive and safe to access it.

终结被调用,并不意味着内存将被回收,如果你做类似下面的的Finalize 将被调用,但内存不会被回收

Finalizer gets called doesn't mean memory will be reclaimed, If you do something like the following Finalize will be called but memory will not be reclaimed

class Test
{
    readonly object _child = new Object();

    private static Test evilInstance;

    ~Test()
    {
        evilInstance = this;//Do something crazy
        //This resurrects this instance, so memory will not be reclaimed.
    }
}

终结,当你处理管理code几乎从来不需要,它增加了额外的工作来的垃圾收集器,也怪事情都可能发生,因为我们上面所看到的。

Finalizers are almost never needed when you're dealing with managed code, It adds extra work to the garbage collector and also strange things can happen as we seen above.

更新:如果您使用 _child 仅供锁定可以安全使用,因为 _child 实例将不能为空,这意味着它指向一个有效的参考。 Monitor.Enter Monitor.Exit 只在乎它是绝对安全的使用它的引用(仅用于锁定)

Update: If you use _child only for lock it is safe to use, because the _child instance will not be null, which means it points to a valid reference. Monitor.Enter and Monitor.Exit just cares about the references it is absolutely safe to use it(only for locking).

如果你需要孩子的终结将只后称之为   测试的终结叫?

What if you need the child's Finalizer to be called only after Test's Finalizer is called?

有一个解决办法:你可以继承孩子的SafeHandle 类和做的伎俩。这将确保,如果两个测试孩子超出范围的同时,它会调用测试的终结首先作为孩子的SafeHandle 这延迟其定稿。但是,IMO不取决于此。因为其他程序员你可能不知道这一点而导致误解工作。

There is a workaround: You can inherit the Child class from SafeHandle and that does the trick. It will make sure if both Test and Child goes out of scope at the same time, It will call Test's finalizer first as the Child inherits from SafeHandle which delays its finalization. But, IMO don't depend on this. Because other programmers work with you may not be knowing this which leads to misconception.

这关键的终结也有弱的排序保证,说明如果一个正常的终结对象和一个关键的终结对象在同一时间变得不可访问,那么正常对象的终结首先运行

This critical finalizer also has a weak ordering guarantee, stating that if a normal finalizable object and a critical finalizable object become unreachable at the same time, then the normal object’s finalizer is run first

的SafeHandle报价:一个可靠性案例研究

这篇关于正子对象还活着的时候Object.Finalize被称为GC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆