Junior
What is a garbage collector in Java?
sobes.tech AI
Answer from AI
Automatic memory management mechanism. It finds and deletes objects that no longer have active references in the program, freeing the memory they occupy.
Main stages of operation:
- Marking: GC identifies reachable objects starting from roots (e.g., local variables, static fields).
- Deletion: GC frees memory occupied by objects that were not marked as reachable.
- Compacting (optional): Some garbage collectors move live objects to eliminate memory fragmentation.
Types of garbage collectors:
- Serial GC
- Parallel GC
- CMS (Concurrent Mark-Sweep) GC
- G1 (Garbage First) GC
- ZGC (Z Garbage Collector)
- Shenandoah GC
The choice of collector affects application performance, memory consumption, and pause times.