什么是类的 GC 根? [英] What are GC roots for classes?

查看:36
本文介绍了什么是类的 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 根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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