Java Zero to All All questionsPractise free
Interview questions › Interview Rapid-Fire — Top Q&A › JVM Internals — Top Interview Q&A

How does Garbage Collection work internally?

Level III · MidJVM Internals — Top Interview Q&A
Short answer

GC reclaims objects that are no longer reachable from GC roots (thread stacks, static fields, JNI references).

Explanation

Modern collectors are generational, built on the weak generational hypothesis: most objects die young. New objects are allocated in the young generation's Eden; a minor GC copies survivors between survivor spaces and ages them, and objects that survive enough cycles are promoted (tenured) to the old generation. Collection uses mark-sweep(-compact) or copying algorithms, often with a Stop-The-World pause to walk the object graph, though concurrent collectors do most marking while the app runs.

Common mistakes
  • Saying GC deletes objects immediately when they are unreferenced.
  • Not explaining generations and reachability from GC roots.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for JVM Internals — Top Interview Q&A.

Practise free

More on JVM Internals — Top Interview Q&A