轉(zhuǎn)載
Java中什么樣的對(duì)象才能作為gc root,gc roots有哪些呢谆棺?
所謂"GC roots", 或者說(shuō)tracing GC的"根集合", 就是一組必須活躍的引用
.
例如說(shuō), 這些引用可能包括:
- 所有Java線程當(dāng)前活躍的棧幀里指向GC堆里的對(duì)象的引用; 換句話說(shuō), 當(dāng)前所有正在被調(diào)用的方法的引用類型的參數(shù)/局部變量/臨時(shí)值.
- VM的一些靜態(tài)數(shù)據(jù)結(jié)構(gòu)里指向GC堆里的對(duì)象的引用, 例如說(shuō)HotSpot VM里的Universe里有很多這樣的引用.
- JNI handles, 包括global handles和local handles
- 所有當(dāng)前被加載的Java類
- Java類的引用類型靜態(tài)變量
- Java類的運(yùn)行時(shí)常量池里的引用類型常量(String或Class類型)
- String常量池(String Table)里的引用
注意, 是一組必須活躍的引用, 不是對(duì)象
Tracing GC的根本思路就是: 給定一個(gè)集合的引用作為根出發(fā), 通過(guò)引用關(guān)系遍歷對(duì)象圖, 能被遍歷到的(可達(dá)到的)對(duì)象就判定為存活, 其余對(duì)象(也就是沒(méi)有被遍歷到的)就自然被判定為死亡. 注意再注意: tracing GC的本質(zhì)是通過(guò)找出所有活對(duì)象來(lái)把其余空間認(rèn)定為"無(wú)用", 而不是找出所有死掉的對(duì)象并回收它們占用的空間.
GC Roots這組引用是tracing GC的起點(diǎn)
. 要實(shí)現(xiàn)語(yǔ)義正確的tracing GC, 就必須要能完整枚舉出所有的GC roots, 否則就可能會(huì)漏掃描應(yīng)該存活的對(duì)象, 導(dǎo)致GC錯(cuò)誤回收了這些被漏掃的活對(duì)象.
目前主流的虛擬機(jī)都是采用GC Roots Tracing算法, 比如Sun的Hotspot虛擬機(jī)便是采用該算法, 該算法的核心算法是從GC Roots對(duì)象作為起始點(diǎn), 利用數(shù)學(xué)中圖論知識(shí), 圖中可達(dá)對(duì)象便是存活對(duì)象, 而不可達(dá)對(duì)象則是需要回收的垃圾內(nèi)存, 這里涉及到兩個(gè)概念: GC Roots
, 可達(dá)性
.
所謂“GC roots”钓猬,或者說(shuō)tracing GC的“根集合”普舆,就是一組必須活躍的引用。
GC Roots的節(jié)點(diǎn): 全局性的引用(常量或靜態(tài)屬性)
表悬、執(zhí)行上下文(例如棧幀中的局部變量表中)
可作為GC Roots的對(duì)象:
// Class loaded by bootstrap/system class loader. For example, everything from the rt.jar like java.util.* .
1.System Class
// Local variable in native code, such as user defined JNI code or JVM internal code.
2.JNI Local
// Global variable in native code, such as user defined JNI code or JVM internal code.
3.JNI Global
// Object referred to from a currently active thread block.
4.Thread Block
// A started, but not stopped, thread.
Thread
// Everything that has called wait() or notify() or that is synchronized.
// For example, by calling synchronized(Object) or by entering a synchronized method.
// Static method means class, non-static method means object.
5.Busy Monitor---用于同步的監(jiān)控對(duì)象
// Local variable. For example, input parameters or locally created objects of methods that are still in the stack of a thread.
6.Java Local
// In or out parameters 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 objects handled as method parameters become GC roots.
// For example, parameters used for file/network I/O methods or reflection.
7.Native Stack
// An object which is in a queue awaiting its finalizer to be run.
8.Finalizable
// An object which has a finalize method, but has not been finalized and is not yet on the finalizer queue.
9.Unfinalized
// An object which is unreachable from any other root,
// but has been marked as a root by MAT to retain objects which otherwise would not be included in the analysis.
10.Unreachable
// A Java stack frame, holding local variables. Only generated when the dump is parsed with the preference set to treat Java stack frames as objects.
11.Java Stack Frame
// An object of unknown root type.
// Some dumps, such as IBM Portable Heap Dump files,
// do not have root information.
// For these dumps the MAT parser marks objects which are have no inbound references or are unreachable from any other root as roots of this type.
// This ensures that MAT retains all the objects in the dump.
12.Unknown
對(duì)于HotSpot VM的GC而言, 不同的GC策略對(duì)于的GC Roots基本一致, 對(duì)于Parallel Scavenge, 實(shí)現(xiàn)上定義了一個(gè)較為明確的RootType枚舉類型
enum RootType {
universe = 1,
jni_handles = 2,
threads = 3,
object_synchronizer = 4,
flat_profiler = 5,
system_dictionary = 6,
class_loader_data = 7,
management = 8,
jvmti = 9,
code_cache = 10
}
關(guān)于可達(dá)性的對(duì)象, 便是能與GC Roots構(gòu)成連通的對(duì)象, 如下圖:
根搜索算法的基本思路就是通過(guò)一系列名為"GC Roots"的對(duì)象作為起始點(diǎn), 從這些節(jié)點(diǎn)開(kāi)始向下搜索, 搜索所走過(guò)的路徑稱為引用鏈(Reference Chain), 當(dāng)一個(gè)對(duì)象到GC Roots沒(méi)有任何引用鏈相連時(shí), 則證明此對(duì)象是不可用的.
從上圖, reference1弥锄、reference2、reference3都是GC Roots, 可以看出:
reference1 -> 對(duì)象實(shí)例1;
reference2 -> 對(duì)象實(shí)例2;
reference3 -> 對(duì)象實(shí)例3;
reference3 -> 對(duì)象實(shí)例4 -> 對(duì)象實(shí)例6;
可以得出對(duì)象實(shí)例1蟆沫、2籽暇、4、6都具有GC Roots可達(dá)性, 也就是存活對(duì)象, 不能被GC回收的對(duì)象.
而對(duì)于對(duì)象實(shí)例3饭庞、5雖然直接連通, 但并沒(méi)有任何一個(gè)GC Roots與之相連, 這便是GC Roots不可達(dá)的對(duì)象, 也就是GC需要回收的垃圾對(duì)象.