Each tree of an object has one or more objects at the root. If the garbage collector can reach the root, the tree is reachable. Any object that is not reached by, or referenced by, GC roots is considered dead and the garbage collector removes it.
Here are the different kinds of GC roots in Java:
- Local variables: Variables or parameters of a Java method.
- Active threads: A running thread is a live object.
- Static variables: Classes referencing static variables. When the garbage collector collects classes, it removes references to static variables.
- JNI references: Object reference created during the JNI call. They are kept alive because JVM is unaware that the native code has references of it.
Please have a look at the following diagram: ...