找回密码
 立即注册
theobjectsreferencegarbage | 软件设计/软件工程 2022-05-02 236 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
一些文章在分析GC时,不可逾越的说到要先从根对象扫描出不可达对象,然后标记那些不可达对象为垃圾。那么源头根对象是什么玩意呢?

几分钟后google到比较可信源是http://stackoverflow.com/questions/8458886/what-is-a-rooted-reference。其解释具体为:

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:



1,A local variable in a method that is currently running(GC发生时,当前正在执行的方法中那些本地变量) 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.

2,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.

3,If a managed object is passed to an unmanaged COM+ library through interop(非托管类似COM+一样通过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.

4,If an object has a finalizer(实现了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.

推荐【译】.Net 垃圾回收机制原理(一),其中也提到了finalizer有重活机会。

(When analyzing GC in some articles, it is insurmountable to scan out unreachable objects from the root object, and then mark those unreachable objects as garbage. So what is the source root object?
A few minutes later, Google found that the more trustworthy source was http://stackoverflow.com/questions/8458886/what-is-a-rooted-reference 。 The explanation is as follows:
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:
1. 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.
2. Static variables are 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.
3. 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.
4. If an object has a finalizer, it is not immediately removed when the garbage collector decisions 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.
Recommend [translation] Net garbage collection mechanism principle (I), which also mentioned that finalizer has a chance to revive.
)





上一篇:LEETCODE-ADD TWO NUMBERS
下一篇:具有静态方法的类(JAVA 中的 MATH 类)