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

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

问题描述

从(<报价href=\"http://stackoverflow.com/questions/8456335/safe-in-c-sharp-not-in-c-simple-return-of-pointer-reference\">Safe在C#中没有C ++的指针/引用简单的返回,通过埃里克利珀回答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根。

必须通过<一读href=\"http://www.simple-talk.com/dotnet/.net-framework/understanding-garbage-collection-in-.net/\">this文章,也许它会帮助您的理解:

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 +不这样做垃圾回收:它使用,相反,引用计数系统;一旦被引用计数设置为0,与对象的COM +库完成它就不再是一个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.

(重点煤矿)

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

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