Junior — Middle
How does the JVM manage automatic garbage collection of unused objects?
sobes.tech AI
Answer from AI
The JVM manages automatic garbage collection of unused objects through a mechanism called Garbage Collection (GC). Objects that are no longer referenced from active code are considered unreachable and are eligible for removal. The JVM periodically runs the garbage collector, which detects such objects and frees the memory they occupy.
Main stages of GC operation:
- Determining object reachability (via reference graph).
- Freeing memory for unreachable objects.
Different JVM implementations may use various algorithms (e.g., Mark-and-Sweep, Generational GC), optimizing performance and minimizing application pauses.