A garbage collection root is an object that is accessible from
outside the heap. The following reasons make an object a GC root:
-
System Class - Class loaded by boot-strap/system class loader. For
example, everything from the rt.jar like
java.util.*
.
- JNI Local - Local variable in native code, such as user defined
JNI code or JVM internal code.
- JNI Global - Global variable in native code, such as user
defined JNI code or JVM internal code.
- Thread Block - Currently active thread.
-
Busy Monitor - 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.
- Java Local - Local variable. For example, input parameters or
locally created objects of methods that are still in the stack of a
thread.
- Native Stack - 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.