Sobes.tech
Senior

What are GC Roots? What other than the stack?

sobes.tech AI

Answer from AI

GC Roots are objects from which the traversal of the object graph in the JVM garbage collector begins to determine reachable objects.

Objects that are considered GC Roots include:

  • Stack variables (Stack frames): local variables and method parameters that are currently active.
  • Static fields of classes: references from static variables.
  • Objects in JNI (Java Native Interface): references held by native code.
  • Objects registered in system classes: for example, ClassLoader, Thread.
  • Active threads: root objects for each thread's stack.

Thus, besides the stack, GC Roots include static fields, JNI references, active threads, and system objects, which ensure object reachability and prevent their collection.

What are GC Roots? What other than the stack? — Java - sobes.tech