Intern
What do you know about Garbage Collector in JVM? What types of GC are there?
sobes.tech AI
Answer from AI
Garbage Collector (GC) in JVM is an automatic memory management mechanism that frees memory occupied by objects that are no longer used by the program.
Main types of GC in JVM:
- Serial GC — a single-threaded collector, simple and efficient for small applications.
- Parallel GC — a multi-threaded collector that speeds up collection through parallel processing.
- CMS (Concurrent Mark Sweep) — a collector with minimal pauses, working concurrently with the application.
- G1 (Garbage First) — a modern collector that divides memory into regions and optimizes collection for large heaps.
GC operates on the principle of dividing memory into generations: Young Generation and Old Generation. The Young Generation is collected more frequently, as most objects quickly become unnecessary.
Example: when an object becomes inaccessible from the code, GC detects this and automatically frees the memory, relieving the programmer from manually managing memory.