Java / Java 17 Garbage Collection Interview Questions
What is garbage collection in Java?
Garbage collection (GC) is the JVM's automatic process for reclaiming heap memory occupied by objects that are no longer reachable from any active part of the running program.
Instead of the developer explicitly freeing memory (as in C's free() or C++'s delete), the JVM periodically runs a background collector that traces object references starting from a set of GC roots - local variables, static fields, active thread stacks, and JNI references - and reclaims anything not reachable from them.
This removes an entire class of bugs (dangling pointers, double frees, use-after-free) at the cost of some runtime overhead and non-deterministic pause timing, since the developer no longer controls exactly when memory is released.
More Related questions...