What are GC roots for classes?(什么是类的 GC 根?)
问题描述
在 Java 中,有称为 垃圾收集根(GC 根)的特殊对象.它们作为垃圾收集标记机制的根对象(见图).
In Java, there are special objects called Garbage Collection Roots (GC roots). They serve as a root objects for Garbage Collection marking mechanism (see picture).
这篇文章介绍了四种类型的GC root:
This article describes four types of GC roots:
- 局部变量
- 活跃的话题
- 静态变量
- JNI 参考
还提到:
类本身可以被垃圾回收.
Classes themselves can be garbage-collected.
没有收集 GC 根,因此类本身不是 GC 根.
GC roots aren't collected thus classes themselves are not GC roots.
那么类的 GC 根是什么?
So what are GC roots for the classes?
推荐答案
那么类的 GC 根是什么?
So what are GC roots for the classes?
类加载器,有效地 - 通过其他 GC 根.
Classloaders, effectively - via other GC roots.
如果没有任何东西可以到达类加载器——这意味着没有任何东西可以到达由该类加载器创建的任何类或这些类的任何实例——那么类加载器和它创建的类都可以进行垃圾回收.在此之前保持它们处于活动状态是必要的,以便 Class::forName 和 ClassLoader::findClass 即使在类的静态初始化器不是时也可以是幂等的.
If there is nothing which can reach a classloader - which means nothing can reach any classes created by that classloader or any instances of those classes - then both the classloader and the classes it created are eligible for garbage collection. Keeping them alive until then is necessary so that Class::forName and ClassLoader::findClass can be idempotent even when the class's static initializers are not.
隐藏类(参见 https://openjdk.java.net/jeps/371)是该规则的例外.作为 OpenJDK 的一个实现细节,使用 java.lang.reflect.Proxy 的静态方法创建的方法引用、lambda 和代理类也是如此.类加载器不持有对这些类的强引用.
Hidden classes (see https://openjdk.java.net/jeps/371) are exceptions to this rule. As an implementation detail of OpenJDK, so are the classes of method references, lambdas, and proxies created with the static methods of java.lang.reflect.Proxy. The classloader does not hold a strong reference to these classes.
这篇关于什么是类的 GC 根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是类的 GC 根?


- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01