Java Garbage Collection Flashcards
Types of GC
Single(Serial) Collector
Parallel Collector
Concurrent Mark and Sweep Collector
Garbage First Garbage Collector ( G1GC)
Serial Collector
Uses single thread to collect the unused objects. This can be useful small memory footprint applications.
java -XX :+UseSerialGC
Parallel Collector
Uses multiple threads to collect.
Might pause application threads also.
Stop the world approach could freeze the application.
java -XX:UseParallelGC
-XX:ParallelGCThreads - number of parallel GC threads
CMS - Concurrent Mark and Sweep
Uses multiple GC threads to collect.
It does not force stop the world, but still affects the application threads.
java -XX:+UseParNewGC
G1 GC
Heap memory is split into multiple regions , and region with lesser free space is selected to clean up the objects.
First it marks the region with unused objects, then sweep phase will kick in for less free space regions.
It is more efficient than CMS.
Java -XX:useG1GC
Java 8 also uses XX:+UseStringDeduplication to deduplicate same string objects to save space.
Generations
Young -> Tenured -> Permanent
Young generation is split into 1 Eden space and 2 survivor spaces. Initially all objects are allocated into eden space.
Minor collection happens to remove dead objects and move live objects to survivor spaces and eventually to Tenured generation.
Permanent -> is for classes, static methods and other language memory footprint.
https://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html
Java Reference Types
Weak
Soft
Phantom
Finalization