1. Overview
1.概述
In this tutorial, we’ll learn what the Garbage Collector Roots (GC roots) are. Additionally, we’ll describe the types of GC roots. Afterward, we’ll show the difference between GC roots and live objects.
在本教程中,我们将学习什么是垃圾收集器的根(GC根)。此外,我们将描述GC根的类型。之后,我们将展示GC根和活对象之间的区别。
2. GC Root Definition
2.GC根的定义
Let’s first define what GC roots are. GC root is a term used in the context of garbage collection in Java. They are special objects for the garbage collector. As the name suggests, GC roots are starting points for the garbage collector processes. In general, all objects directly or indirectly referenced from a GC root are not garbage collected.
首先让我们定义一下什么是GC根。GC根是在Java中垃圾收集的背景下使用的术语。它们是垃圾收集器的特殊对象。顾名思义,GC根是垃圾收集器进程的起点。一般来说,所有直接或间接从GC根引用的对象都不会被垃圾收集。
3. Types of GC Roots
3.GC根的类型
Let’s have a look at the main types of GC Roots:
让我们来看看GC根的主要类型。
- Class: Classes loaded by a system class loader; contains references to static variables as well
- Stack Local: Local variables and parameters to methods stored on the local stack
- Active Java Threads: All active Java threads
- JNI References: Native code Java objects created for JNI calls; contains local variables, parameters to JNI methods, and global JNI references
Additionally, there are a few more possible types of GC Roots:
此外,还有一些可能的GC根的类型。
- Objects used as monitors for synchronization
- Specific objects defined by the JVM implementation that are not garbage collected for its purpose. That might contain important exception classes, system class loaders, or custom class loaders
Furthermore, there is no documentation per JVM about which specific objects are GC roots. Some of the popular Java IDEs provide the functionality to analyze memory from the GC roots perspective. This is beneficial when analyzing memory leaks in an application.
此外,每个JVM都没有关于哪些具体对象是GC根的文档。一些流行的 Java IDE 提供了从 GC 根部角度分析内存的功能。在分析应用程序中的内存泄漏时,这是很有益的。
4. GC Roots vs. Live Objects
4.GC根与活物
Let’s now have a look at the live objects defined in the garbage collection process.
现在让我们来看看垃圾收集过程中定义的活体对象。
All objects actively used by an application are live objects for GC. Furthermore, the garbage collector doesn’t delete live objects. GC roots are a special type of live object. Therefore, all GC roots are live objects by definition.
所有被应用程序积极使用的对象都是GC的活对象。此外,垃圾收集器不会删除实时对象。GC根是一种特殊类型的活对象。因此,根据定义,所有GC根都是活对象。
5. Garbage Collector Usage of GC Roots
5.GC根的垃圾收集器的使用
Let’s now have a look at the usage of GC Roots in the garbage collection process.
现在让我们来看看GC根在垃圾收集过程中的用法。
As a matter of fact, all GC implementations in the HotSpot JVM are tracing collectors. GC identifies all live objects by traversing the objects graph. In addition, objects visited and marked as alive won’t be garbage collected. To be able to traverse the graph, starting points are necessary. Thus, GC roots are starting points for tracing collectors.
事实上,HotSpot JVM中的所有GC实现都是tracing collectors。GC通过遍历对象图来识别所有活着的对象。此外,被访问并标记为活的对象不会被垃圾收集。为了能够遍历图,起点是必要的。因此,GC的根是追踪收集器的起点。
GC starts traversing the graph from the root and marks all visited objects in the graph as alive. The process executes for every GC root defined in an application. Moreover, it processes all graphs starting from all GC roots. Then, it marks all visited objects as alive. After that, all objects that haven’t been visited are garbage collected.
GC从根部开始遍历图,并将图中所有被访问的对象标记为活的。这个过程对应用程序中定义的每一个GC根都会执行。此外,它还处理从所有GC根开始的所有图。然后,它将所有访问过的对象标记为活着。之后,所有没有被访问过的对象都被垃圾回收。
6. Conclusion
6.结论
In this short article, we explained what GC roots are. Then, we described the types of GC roots. Next, we showed the difference between GC roots and live objects. Finally, we explained the usage of GC roots in the garbage collection process.
在这篇短文中,我们解释了什么是GC根。然后,我们描述了GC根的类型。接下来,我们展示了GC根和实时对象之间的区别。最后,我们解释了GC根在垃圾收集过程中的用法。