Have you ever seen an OutOfMemoryError? I'm sure you have. But then, did you understood that line you copied from StackOverflow?
If you haven't, and if you want a gently introduction to the complex world of Java Garbage Collection this is your talk.
I'll talk about garbage collection concepts, the garbage collection in the Hotspot JVM (the default in Oraclel's JDK/JRE) and I'll try to put it in terms that any Java developer can grasp. The next time you'll face the dreaded 'OutOfMemoryError', at least, you'll know what are you up against.
8. Java needs lots of memory!!!
Java is so slow!!!!!!
Garbage collection is
wrong!!!!!
9. With CMS GC, the full collection is serial and STW,
hence your application threads are stopped for the
entire duration while the heap space is reclaimed
and then compacted.
The duration for the STW pause depends on your
heap size and the surviving objects.
http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All
10. With CMS GC, the full collection is serial and STW,
hence your application threads are stopped for the
entire duration while the heap space is reclaimed
and then compacted.
The duration for the STW pause depends on your
heap size and the surviving objects.
http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All
59. - Global stop of every thread to execute a
garbage collection
- The opposite would be CONCURRENT
○ GC executed at the same time as the
program
Stop the World (STW)
81. ● Serial Collector
● Parallel Collector
● CMS Collector
● G1 (Garbage First) Collector
GC’s en OpenJDK Hotspot
82. YOUNG GENERATION OLD GENERATION
Parallel Serial + Parallel
Monolithic Incremental
STW STW + Concurrent
Copying Mark and Sweep
Concurrent Mark & Sweep
83. 1. Initial Mark
2. Concurrent Mark
3. Remark
4. Concurrent Sweep
Concurrent Mark & Sweep
89. ● Serial Collector
● Parallel Collector
● CMS Collector
● G1 (Garbage First) Collector
GC’s en OpenJDK Hotspot
90. - Delay as much as possible a “Full GC”
- Low latency
- Predictability
- Easy to use
Garbage First (G1)
91. - Divides the heap in regions
- 2048 regions. Size: heap / 2048 ~ nearest factor of 2
- Frees first the regions with more garbage
(Garbage First)
- Compacts while moving
Garbage First (G1)
105. 1. Collects the Young Generation
2. Concurrent marking
3. Mixed collection
4. Evacuation failure
Garbage First (G1)
106. Evacuation failure
O
O
O
O
O O
O
O
O
O
S O
O
O
O
O
O
O O
O
OO O
O
O
O
O
O
O
O
O
OO
E
E
E
Not enough space for collect the young or
survivors. What happens then?
109. All the GCs in Oracle’s JVM
eventually Stop The World
110. Special treatment for “huge” objects
- Objects > 50% region size
- Special Humongous region
- Not moved between regions: More fagmentation!
- If using G1 try to avoid huge objects
G1 - Humongous objects
112. - Region Size = Heap Size / 2048
- Humongous Threshold = Region Size / 2
If your Heap is very small, every object can
become humongous!
G1 - Humongous objects
114. CMS vs G1
Minimize duration of STW Minimize quantity of STW
Throughput Latency
Old New-ish
Better for small heaps The best for large heaps
Fragmentation Compacts
115. CMS vs G1
Bottom line:
- G1 is the new default GC for Java 9
- Long term replacement for the CMS
If you want throughput Parallel GC it’s better
155. 1. Structure of JVM’s memory
2. Concepts about Garbage Collection
3. Types of collectors in Oracle’s Hotspot
4. Configurations inside JVM about GC
156. With CMS GC, the full collection is serial and STW,
hence your application threads are stopped for the
entire duration while the heap space is reclaimed
and then compacted.
The duration for the STW pause depends on your
heap size and the surviving objects.
http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All