GC ROOTS

GC ROOTS(GarbageCollection Roots)垃圾回收根对象集合,是JVM在做垃圾回收时,判断可达到性的根遍历集合。

通过GC ROOTS可以查看哪些对象可以继续存活下来。

 

 

一、 类加载器

 

System Class

Classloaded by bootstrap/system class loader. For example, everything from thert.jar like java.util.* .

根加载器、系统加载器加载的类对象。静态变量引用。

 

二、 编码过程中可以看得到的

Thread Block

Objectreferred to from a currently active thread block.

Thread

A started,but not stopped, thread.

Busy Monitor

Everythingthat has called wait() or notify() or that is synchronized.For example, by calling synchronized(Object) or by entering asynchronized method. Static method means class, non-static method means object.

 

活动线程引用对象集合。

线程,启动但没有停止的线程(运行,挂起,阻塞等),相关的对象集合。

与对象锁相关的对象集合。

 

Java Local

Localvariable. For example, input parameters or locally created objects of methodsthat are still in the stack of a thread.

本地变量引用对象。

 

Native Stack

In or outparameters in native code, such as user defined JNI code or JVM internal code.This is often the case as many methods have native parts and the objectshandled as method parameters become GC roots. For example, parameters used forfile/network I/O methods or reflection.

JNI Local

Localvariable in native code, such as user defined JNI code or JVM internal code.

JNI Global

Globalvariable in native code, such as user defined JNI code or JVM internal code.

本地方法栈对象集合,

JNI本地对象,和全局对象,本地栈中的对象集合。

 

三、 JVM内部处理编码过程不可见的

Finalizable

An objectwhich is in a queue awaiting its finalizer to be run.

Unfinalized

An objectwhich has a finalize method, but has not been finalized and is not yet on thefinalizer queue.

 

拥有finalize()方法的对象,既没有放到finalizer queue队列,也没被调用finalize()的对象。

放到finalizerqueue队列中的对象。

 

Java Stack Frame

A Javastack frame, holding local variables. Only generated when the dump is parsedwith the preference set to treat Java stack frames as objects.

栈帧对象。

 

Unknown

An objectof unknown root type. Some dumps, such as IBM Portable Heap Dump files, do nothave root information. For these dumps the MAT parser marks objects which arehave no inbound references or are unreachable from any other root as roots ofthis type. This ensures that MAT retains all the objects in the dump.

未知对象。例如,IBMHeap Dump files.

 

MAT(Memory Analyzer Tool)

Unreachable

An objectwhich is unreachable from any other root, but has been marked as a root by MATto retain objects which otherwise would not be included in the analysis.

被内存分析工具标记后的对象。

### GC Roots 的概念及其在 Java 内存管理中的作用 #### 一、GC Roots 的定义 GC Roots 是指一组特殊的对象引用,在垃圾回收过程中,这些对象被认为是不可回收的。它们充当了整个对象图的起点,所有的可访问对象都必须可以通过某种方式从这些根对象间接到达[^1]。 #### 二、GC Roots 的重要性 JVM 使用 GC Roots 来判断哪些对象是可以被安全回收的。具体来说,任何无法通过 GC Roots 到达的对象都会被视为垃圾,并最终由垃圾回收器清理掉。这种方法有效解决了内存泄漏问题的同时也提高了内存利用率[^5]。 #### 三、常见的 GC Roots 类型 以下是几种典型的可以作为 GC Roots 的对象类别: - **虚拟机栈(Stack Frames)中的局部变量表**:包括方法参数、返回地址以及一些临时变量等。 - **本地方法栈中JNI(即Native Method)的引用**:当Java程序调用了C/C++库函数时产生的跨语言交互部分也会成为GC Root的一部分。 - **方法区内的类静态属性**:比如`static final String str = "example";`这里的字符串实例就属于此类别之一[^4]。 - **运行时常量池里的引用**:例如String类型的字面量或者Class对象本身都是潜在候选者[^2]。 #### 四、如何利用 GC Roots 实现垃圾检测? 为了找出那些应该被淘汰的目标实体,JVM采用了基于可达性的算法——即从所有现存的有效GC Roots出发遍历整张对象图表结构;凡是未能触及到的部分都将标记为废弃状态等待后续处理阶段予以清除操作执行完毕之后再重新整理剩余存活下来的个体集合以便于下一轮循环继续运作下去[^3]。 ```java // 示例代码展示了一个简单的场景,其中涉及到了GC Roots的作用机制 public class GCTest { private Object instance; protected void finalize() throws Throwable { System.out.println("Object is going to be collected."); } public static void main(String[] args) { GCTest objA = new GCTest(); GCTest objB = new GCTest(); objA.instance = objB; objB.instance = objA; objA = null; objB = null; // 提示系统进行垃圾收集 System.gc(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } ``` 上述例子展示了两个相互持有对方成员变量的情况,尽管如此,一旦失去了来自外部强引用的支持,则仍然会被判定成孤立子网而遭到摧毁命运降临其上。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值