Sobes.tech
Junior — Middle

Can you explain what are garbage collector roots and what types exist?

sobes.tech AI

Answer from AI

GC roots are objects from which the traversal of the object graph begins to determine reachability in memory. The garbage collector starts from the roots and marks all objects reachable from them as alive. Objects that are unreachable from the roots are considered garbage and are eligible for removal.

Types of roots typically include:

  • Local variables and parameters of current threads (call stack).
  • Static fields of classes.
  • Active threads.
  • Objects registered in native code (e.g., JNI).

For example, in Android, roots can be objects referenced by active Activities, static fields, or objects on the current thread's stack.