- GC (Garbage Collection) is the memory management process for objects in the heap.
- GC contains 3 phases — marking, deletion, copying/compaction.
- Marking: marks everything either as live (referenced) objects, unreferenced objects or available memory space.
- Deletion: Unreferenced objects are deleted.
- Compaction: Remaining objects are compacted. - Objects are aging through these 3 phases.
- GC can cause performance issues. So, properly using/knowing garbage collection is import to manage environment.
- GC perfomance Concerns
- Memory leaks: Because GC only stops unreferenced objects, memory can be fulled with referenced objects that is no longer in used.
- Continuous “Stop the World” Events: For executing GC, all threads in the JVM are stopped. It is expected there is low impact but this can cause performance issue.
- CPU usage: Continuous “Stop the World” can cause a spike in CPU usage. - How does GC work?
When Objects allocated in heap → Places in Eden space → marked (referenced, unreferenced) → deletion → compacted → … (Loop)
: https://www.youtube.com/watch?v=DQgwQYjjyuA&t=30s - There are few different kind of Java GC.
- G1 Collector(Default GC in Java 9)
- CMS Collector (Concurrent Mark Sweep)
- Serial Collector
- Parallel Collector - Improving GC performance
- Adjust the heap sizes of young and old generations.
- Reduce the rate of object allocation and promotion.
🍰