Junior — Middle
How does the garbage collector determine that an object is no longer used by the program?
sobes.tech AI
Answer from AI
The garbage collector determines that an object is no longer used by the program based on the fact that there are no active references to it from root objects (root set). Usually, this means the object is unreachable from call stacks, global variables, or other root access points. If the object is unreachable, the program cannot access it, and its memory can be freed.
Example: In Android (Java/Kotlin), the garbage collector uses graph traversal algorithms starting from the roots and marks all reachable objects. Everything not marked is considered garbage and is removed.