Garbage collection roots

本文介绍了垃圾回收根节点的概念及分类,包括系统类、JNI本地变量、线程块等,并解释了不可达对象如何被标记及如何在内存分析中处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Garbage collection roots

A garbage collection root is an object that is accessible from outside the heap.

Memory Analyzer categorizes garbage collection roots according to the following list:
System class
A class that was loaded by the bootstrap loader, or the system class loader. For example, this category includes all classes in the  rt.jar file (part of the Java runtime environment), such as those in the  java.util.* package.
JNI local
A local variable in native code, for example user-defined JNI code or JVM internal code.
JNI global
A global variable in native code, for example user-defined JNI code or JVM internal code.
Thread block
An object that was referenced from an active thread block.
Thread
A running thread.
Busy monitor
Everything that called the  wait() or  notify() methods, or that is synchronized, for example by calling the  synchronized(Object) method or by entering a synchronized method. If the method was static, the root is a class, otherwise it is an object.
Java local
A local variable. For example, input parameters, or locally created objects of methods that are still in the stack of a thread.
Native stack
Input or output parameters in native code, for example user-defined JNI code or JVM internal code. Many methods have native parts, and the objects that are handled as method parameters become garbage collection roots. For example, parameters used for file, network, I/O, or reflection operations.
Finalizer
An object that is in a queue, waiting for a finalizer to run.
Unfinalized
An object that has a finalize method, but was not finalized, and is not yet on the finalizer queue.
Unreachable
An object that is unreachable from any other root, but was marked as a root by Memory Analyzer so that the object can be included in an analysis.

Unreachable objects are often the result of optimizations in the garbage collection algorithm. For example, an object might be a candidate for garbage collection, but be so small that the garbage collection process would be too expensive. In this case, the object might not be garbage collected, and might remain as an unreachable object.

By default, unreachable objects are excluded when Memory Analyzer parses the heap dump. These objects are therefore not shown in the histogram, dominator tree, or query results. You can change this behavior by clicking File > Preferences... > IBM Diagnostic Tools for Java - Memory Analyzer, then selecting the Keep unreachable objects check box.

Java stack frame
A Java stack frame, which holds local variables. This type of garbage collection root is only generated if you set the Preferences to treat Java stack frames as objects. For more information, see  Java Basics: Threads and thread stack queries.
Unknown
An object of unknown root type. Some dumps, such as IBM Portable Heap Dump ( .phd) files, do not have root information. In this case, the Memory Analyzer parser marks objects that have no inbound references, or are unreachable from any other root, as unknown. This action ensures that Memory Analyzer retains all the objects in the dump.

转载自:
http://pic.dhe.ibm.com/infocenter/hctool/v1r0/index.jsp?topic=%2Fcom.ibm.java.diagnostics.memory.analyzer.doc%2Fgcroots.html&resultof%3D%2522%2547%2561%2572%2562%2561%2567%2565%2522%2520%2522%2567%2561%2572%2562%2561%2567%2522%2520%2522%2543%256f%256c%256c%2565%2563%2574%2569%256f%256e%2522%2520%2522%2563%256f%256c%256c%2565%2563%2574%2522%2520%2522%2552%256f%256f%2574%2573%2522%2520%2522%2572%256f%256f%2574%2522%2520   



垃圾回收(Garbage Collection)是Java中的一种自动内存管理机制。它的目标是通过自动检测和回收不再使用的对象来释放内存,以避免内存泄漏和提高程序的性能。 在Java中,当创建对象时,内存会被分配给对象,并且当对象不再被引用时,这部分内存将成为垃圾。垃圾回收器负责识别和回收这些垃圾对象,并将内存释放回系统供其他对象使用。 垃圾回收器使用了一些算法来确定哪些对象是可回收的。其中最常用的算法是"引用计数"和"可达性分析"。引用计数算法会为每个对象维护一个引用计数器,当对象被引用时计数器加1,当对象不再被引用时计数器减1。当计数器为0时,该对象被标记为垃圾并进行回收。 而可达性分析算法则是从一组称为"GC Roots"的根对象开始,递归地遍历所有对象,并标记所有可达的对象。未被标记的对象即被认为是不可达的垃圾对象,将被回收。 一旦标记阶段完成,垃圾回收器会执行垃圾回收操作。这个过程包括内存的整理和回收。内存整理是将存活的对象移到一侧,以便为新对象提供连续的内存空间,从而减少内存碎片化。回收操作则是将未被标记的垃圾对象释放,将内存返回给系统。 垃圾回收是Java的一项重要特性,通过自动管理内存,开发人员可以专注于业务逻辑而不必手动进行内存管理。然而,垃圾回收也会带来一些性能开销,因此了解其工作原理以及如何优化和调整垃圾回收器的行为对于开发高性能的Java应用程序至关重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值