什么是“有根参考"? [英] What is a "rooted reference"?

查看:18
本文介绍了什么是“有根参考"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用自 ( 安全C# 不在 C++ 中,指针/引用的简单返回, Eric lippert 的回答 3).

Quote from ( Safe in C# not in C++, simple return of pointer / reference, answer 3) by Eric lippert.

另外,请注意,不是对 Person 对象的任何引用使其保持活动状态.引用必须有根.您可以有两个相互引用但无法访问的 Person 对象;每个人都有一个引用的事实并不能使它们保持活力;其中一个引用必须根.

Also, note that it is not any reference to the Person object that keeps it alive. The reference has to be rooted. You could have two Person objects that reference each other but are otherwise unreachable; the fact that each has a reference does not keep them alive; one of the references has to be rooted.

我不明白,有人能解释一下什么是根引用吗?

I dont understand, can someone explain what a rooted reference is?

推荐答案

表示 GC 根.

阅读这个文章,也许它会帮助你理解:

Have a read through this article, maybe it will help with your understanding:

GC 根本身不是对象,而是对对象的引用.GC 根引用的任何对象都将自动在下一次垃圾收集中幸存下来..NET 中有四种主要的根:

GC roots are not objects in themselves but are instead references to objects. Any object referenced by a GC root will automatically survive the next garbage collection. There are four main kinds of root in .NET:

当前正在运行的方法中的局部变量被认为是 GC 根.这些变量引用的对象总是可以通过声明它们的方法立即访问,因此必须保留它们.这些根的生命周期取决于程序的构建方式.在调试版本中,只要方法在堆栈上,局部变量就会持续存在.在发布版本中,JIT 能够查看程序结构以计算出该方法可以使用的变量在执行过程中的最后一个点,并在不再需要时将其丢弃.这种策略并不总是使用,可以关闭,例如,通过在调试器中运行程序.

A local variable in a method that is currently running is considered to be a GC root. The objects referenced by these variables can always be accessed immediately by the method they are declared in, and so they must be kept around. The lifetime of these roots can depend on the way the program was built. In debug builds, a local variable lasts for as long as the method is on the stack. In release builds, the JIT is able to look at the program structure to work out the last point within the execution that a variable can be used by the method and will discard it when it is no longer required. This strategy isn’t always used and can be turned off, for example, by running the program in a debugger.

静态变量也总是被认为是 GC 根.它们引用的对象可以随时被声明它们的类(或程序的其余部分,如果它们是公共的)访问,因此 .NET 将始终保留它们.声明为线程静态"的变量只会在该线程运行时持续存在.

Static variables are also always considered GC roots. The objects they reference can be accessed at any time by the class that declared them (or the rest of the program if they are public), so .NET will always keep them around. Variables declared as ‘thread static’ will only last for as long as that thread is running.

如果一个托管对象通过互操作传递给一个非托管的 COM+ 库,那么它也将成为一个带有引用计数的 GC 根.这是因为 COM+ 不进行垃圾收集:而是使用引用计数系统;一旦 COM+ 库通过将引用计数设置为 0 来完成对象的处理,它就不再是 GC 根,可以再次收集.

If a managed object is passed to an unmanaged COM+ library through interop, then it will also become a GC root with a reference count. This is because COM+ doesn’t do garbage collection: It uses, instead, a reference counting system; once the COM+ library finishes with the object by setting the reference count to 0 it ceases to be a GC root and can be collected again.

如果一个对象有一个终结器,当垃圾收集器确定它不再活跃"时,它不会立即被删除.相反,它成为一种特殊的根,直到 .NET 调用终结器方法.这意味着这些对象通常需要多次垃圾回收才能从内存中删除,因为它们将在第一次被发现未使用时继续存在.

If an object has a finalizer, it is not immediately removed when the garbage collector decides it is no longer ‘live’. Instead, it becomes a special kind of root until .NET has called the finalizer method. This means that these objects usually require more than one garbage collection to be removed from memory, as they will survive the first time they are found to be unused.

(强调我的)

这篇关于什么是“有根参考"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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