Slide 1

Slide 1 text

JAVA GARBAGE COLLECTION - THE BASICS @CGuntur CHANDRA GUNTUR http://cguntur.me

Slide 2

Slide 2 text

@CGuntur 2 About • Java Champion • JCP Executive Committee Rep. for BNY Mellon • Programming in Java since 1998 • JUG Leader @ NYJavaSIG and NJJavaSIG • Creator of Java-Katas Github repository • Ardent blogger and tweeter • Saganist (with a ‘g’ not a ‘t’)

Slide 3

Slide 3 text

@CGuntur What we cover • Overview and history of garbage collection • Types of garbage collection • General patterns • Deeper dive into Generational GCs • The newer Garbage First GC • Recently added garbage collectors 3

Slide 4

Slide 4 text

@CGuntur 4 • Overview and history of garbage collection • Types of garbage collection • General patterns • Deeper dive into Generational GCs • The newer Garbage First GC • Recently added garbage collectors

Slide 5

Slide 5 text

@CGuntur What is Garbage Collection? 5

Slide 6

Slide 6 text

@CGuntur What is Garbage Collection? As a broad definition, garbage collection is: 5

Slide 7

Slide 7 text

@CGuntur What is Garbage Collection? As a broad definition, garbage collection is: • looking up a managed memory area • identify objects in-use as live objects • mark objects no longer used as garbage 5

Slide 8

Slide 8 text

@CGuntur What is Garbage Collection? As a broad definition, garbage collection is: • looking up a managed memory area • identify objects in-use as live objects • mark objects no longer used as garbage • [occasionally] reclaim memory by deleting garbage 5

Slide 9

Slide 9 text

@CGuntur What is Garbage Collection? As a broad definition, garbage collection is: • looking up a managed memory area • identify objects in-use as live objects • mark objects no longer used as garbage • [occasionally] reclaim memory by deleting garbage • [occasionally] compact memory by defragmenting 5

Slide 10

Slide 10 text

@CGuntur History First garbage collection process: Lisp When: 1959 Author: John MacCarthy His other contributions: • Author of Lisp • Major contributor to ALGOL • A founding father in the AI space 6

Slide 11

Slide 11 text

@CGuntur 7 • Overview and history of garbage collection • Types of garbage collection • General patterns • Deeper dive into Generational GCs • The newer Garbage First GC • Recently added garbage collectors

Slide 12

Slide 12 text

@CGuntur Classifications Focus on the four classifications: 1. based on collection type 2. based on object marking 3. based on execution volume (run interval) 4. based on space compaction 8

Slide 13

Slide 13 text

@CGuntur 1. Based on collection type • Serial collection • Parallel collection • Concurrent collection 9 Classification based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 14

Slide 14 text

@CGuntur Serial Collection 10 Classification - based on collection type App threads GC thread App threads based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 15

Slide 15 text

@CGuntur Serial Collection • Single thread collects • Halts all application threads • STOP-THE-WORLD collector • Real-life analogy (Mail box): • Stop incoming mail • Single person checks current mails 11 Classification - based on collection type based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 16

Slide 16 text

@CGuntur Parallel Collection 12 Classification - based on collection type App threads GC threads App threads based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 17

Slide 17 text

@CGuntur Parallel Collection • Many threads collects • Halts all application threads • STOP-THE-WORLD collector • Real-life analogy (Mail box): • Stop incoming mail • Many people check current mails 13 Classification - based on collection type based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 18

Slide 18 text

@CGuntur Concurrent Collection 14 Classification - based on collection type App threads GC threads App threads based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 19

Slide 19 text

@CGuntur Concurrent Collection • Many threads collects • Application threads can continue • CONCURRENT collector • Real-life analogy (Mail box): • Do not stop incoming mail • One or more people check current mails 15 Classification - based on collection type based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 20

Slide 20 text

@CGuntur 2. Based on object marking • Precise collection • Conservative collection 16 Classification based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 21

Slide 21 text

@CGuntur Precise collection 17 Classification - based on object marking based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 22

Slide 22 text

@CGuntur Precise collection • All objects identified • On finish, objects are live or garbage. • Typically slow • Aims for thoroughness not speed • Real-life analogy (Mail box): • check all mail • discard fliers and junk mail 18 Classification - based on object marking based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 23

Slide 23 text

@CGuntur Conservative collection 19 Classification - based on object marking based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 24

Slide 24 text

@CGuntur Conservative collection • Not all objects identified • If not identified, object considered live. • Typically fast • Aims for speed not thoroughness • Real-life analogy (Mail box): • check mail, find fliers and discard • assume all addressed mail is not junk 20 Classification - based on object marking based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 25

Slide 25 text

@CGuntur 3. Based on execution volume • All-at-once collection • Incremental collection 21 Classification based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 26

Slide 26 text

@CGuntur All-at-once collection 22 Classification - based on execution volume based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 27

Slide 27 text

@CGuntur • Entire collection at one go • Real-life analogy (Mail box): • check all current mail at once All-at-once collection 23 Classification - based on execution volume based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 28

Slide 28 text

@CGuntur Incremental collection 24 Classification - based on execution volume based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 29

Slide 29 text

@CGuntur • Slices of memory area collected • Real-life analogy (Mail box): • pick a few mails at a time and check Incremental collection 25 Classification - based on execution volume based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 30

Slide 30 text

@CGuntur 4. Based on space compaction • Non-moving collection • Moving collection 26 Classification based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 31

Slide 31 text

@CGuntur Non-moving collection 27 Classification - based on space compaction White = Dead objects Black = Live objects based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 32

Slide 32 text

@CGuntur • Live objects not moved • Live objects stay fragmented • Aims for speed not contiguous space • Real-life analogy (Mail box): • remove fliers and junk mail • leave other mail as is, still cluttered Non-moving collection 28 Classification - based on space compaction based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 33

Slide 33 text

@CGuntur Moving collection 29 Classification - based on space compaction White = Dead objects Black = Live objects based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 34

Slide 34 text

@CGuntur • Live objects moved to new location • Live objects contiguously placed • Aims for contiguous space not speed • Real-life analogy (Road repair): • remove junk mail • stack remaining mail to make space for new Moving collection 30 Classification - based on space compaction based on collection type based on object marking based on execution volume (run interval) based on space compaction

Slide 35

Slide 35 text

@CGuntur 31 • Overview and history of garbage collection • Types of garbage collection • General patterns • Deeper dive into Generational GCs • The newer Garbage First GC • Recently added garbage collectors

Slide 36

Slide 36 text

@CGuntur Definitions • What is a Root Object? • What is tri-color marking? 32

Slide 37

Slide 37 text

@CGuntur What is Garbage Collection? As a broad definition, garbage collection is: • looking up a managed memory area • identify objects in-use as live objects • mark objects no longer used as garbage • [occasionally] reclaim memory by deleting garbage • [occasionally] compact memory by defragmenting 33 Garbage collection - Recap

Slide 38

Slide 38 text

@CGuntur What is Garbage Collection? As a broad definition, garbage collection is: • looking up a managed memory area • identify objects in-use as live objects • mark objects no longer used as garbage • [occasionally] reclaim memory by deleting garbage • [occasionally] compact memory by defragmenting 33 Garbage collection - Recap

Slide 39

Slide 39 text

@CGuntur Root object: Object accessible outside the heap • System class • Local variable • Thread • Many more … Definition 34 https://www.yourkit.com/docs/java/help/gc_roots.jsp

Slide 40

Slide 40 text

@CGuntur Tri-color marking: Painting objects with colors • Gray = Object needs evaluation • Black = Object evaluated as live • White = Object evaluated as garbage Definition 35

Slide 41

Slide 41 text

@CGuntur Patterns of GC Three common patterns of garbage collection: 1. Reference Counting Pattern 2. Mark/Sweep[/Compacting] Pattern 3. Copying Pattern 36

Slide 42

Slide 42 text

@CGuntur Reference Counting 37 Patterns in GC 1 2 1 1 2 1 1 1 1 1 1 1 1 1

Slide 43

Slide 43 text

@CGuntur Reference Counting 37 Patterns in GC 1 2 1 1 2 1 1 1 1 1 1 1 1 1

Slide 44

Slide 44 text

@CGuntur Mark/Sweep[/Compacting] 38 Patterns in GC Free Space GC Begin O1 O2 O3 O4 O5 O6 Objects are marked in Gray to be evaluated

Slide 45

Slide 45 text

@CGuntur Mark/Sweep[/Compacting] 38 Patterns in GC Free Space GC Begin O1 O2 O3 O4 O5 O6 Objects are marked in Gray to be evaluated Free Space Mark O1 O2 O3 O4 O5 O6 Live objects are marked Black, Dead objects are marked White

Slide 46

Slide 46 text

@CGuntur Mark/Sweep[/Compacting] 38 Patterns in GC Free Space GC Begin O1 O2 O3 O4 O5 O6 Objects are marked in Gray to be evaluated Free Space Mark O1 O2 O3 O4 O5 O6 Live objects are marked Black, Dead objects are marked White Free Space Sweep O1 O3 O5 O6 Dead objects are swept off

Slide 47

Slide 47 text

@CGuntur Mark/Sweep[/Compacting] 38 Patterns in GC Free Space GC Begin O1 O2 O3 O4 O5 O6 Objects are marked in Gray to be evaluated Free Space Mark O1 O2 O3 O4 O5 O6 Live objects are marked Black, Dead objects are marked White Free Space Sweep O1 O3 O5 O6 Dead objects are swept off Free Space Compact O1 O3 O5 O6 [Optional step] Compact objects to create contiguous free space

Slide 48

Slide 48 text

@CGuntur Copying 39 Patterns in GC “From” . Memory O1 O2 O3 O4 O5 O6 Space divided into 2 equal size areas (From and To regions) “To” Region No Objects allocated here

Slide 49

Slide 49 text

@CGuntur Copying 39 Patterns in GC “From” . Memory O1 O2 O3 O4 O5 O6 Space divided into 2 equal size areas (From and To regions) “To” Region No Objects allocated here GC Begin O1 O2 O3 O4 O5 O6 Objects are marked in Gray to be evaluated “From” . “To” Region No Objects allocated here

Slide 50

Slide 50 text

@CGuntur Copying 39 Patterns in GC “From” Region Free Space O1 O2 O3 O4 O5 O6 Copy Live objects marked Black copied to To region O1 O3 O5 O6 “From” . Memory O1 O2 O3 O4 O5 O6 Space divided into 2 equal size areas (From and To regions) “To” Region No Objects allocated here GC Begin O1 O2 O3 O4 O5 O6 Objects are marked in Gray to be evaluated “From” . “To” Region No Objects allocated here

Slide 51

Slide 51 text

@CGuntur Copying 39 Patterns in GC “From” Region Free Space O1 O2 O3 O4 O5 O6 Copy Live objects marked Black copied to To region O1 O3 O5 O6 “From” . Memory O1 O2 O3 O4 O5 O6 Space divided into 2 equal size areas (From and To regions) “To” Region No Objects allocated here “From” Region Free Space Purge From region is purged. Current To region becomes new From region O1 O3 O5 O6 New “To” Region No Objects allocated here GC Begin O1 O2 O3 O4 O5 O6 Objects are marked in Gray to be evaluated “From” . “To” Region No Objects allocated here

Slide 52

Slide 52 text

@CGuntur Comparing M/S[/C] and Copying 40 Patterns in GC Mark/Sweep[/Compact] Copying efficient for larger memory areas efficient for small memory areas works best on longer-lived objects works best on short-lived objects incremental or all-at-once collector all-at-once collector no extra free space needed to run needs 2x the memory space to run concurrent e.g. Concurrent Mark Sweep
 stop-the-world e.g. Mark Sweep Compact stop-the-world collector

Slide 53

Slide 53 text

@CGuntur 41 • Overview and history of garbage collection • Types of garbage collection • General patterns • Deeper dive into Generational GCs • The newer Garbage First GC • Recently added garbage collectors

Slide 54

Slide 54 text

@CGuntur Generational GC • Default GC in Java since Java 5 • Divides memory into smaller areas • Collection patterns different per area • Based on Weak Generational Hypothesis: • Most objects don’t have a long life • Older gen. objects rarely reference younger gen. objects 42

Slide 55

Slide 55 text

@CGuntur 43 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. }Young Generation }Old Generation }Class definitions, statics etc.

Slide 56

Slide 56 text

@CGuntur 43 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. }Young Generation }Old Generation }Class definitions, statics etc. Initial Size

Slide 57

Slide 57 text

@CGuntur 43 Generational GC Eden Survivor 1 Survivor 2 Tenured Reserved Reserved Permanent Gen. Reserved }Young Generation }Old Generation }Class definitions, statics etc. Initial Size

Slide 58

Slide 58 text

@CGuntur 43 Generational GC Eden Survivor 1 Survivor 2 Tenured Reserved Reserved Permanent Gen. Reserved }Young Generation }Old Generation }Class definitions, statics etc. Initial Size Max. Size

Slide 59

Slide 59 text

@CGuntur 43 Generational GC Eden Survivor 1 Survivor 2 Tenured Reserved Reserved Permanent Gen. Reserved }Young Generation }Old Generation }Class definitions, statics etc. Initial Size Max. Size Minor GC

Slide 60

Slide 60 text

@CGuntur 43 Generational GC Eden Survivor 1 Survivor 2 Tenured Reserved Reserved Permanent Gen. Reserved }Young Generation }Old Generation }Class definitions, statics etc. Initial Size Max. Size Minor GC Major GC

Slide 61

Slide 61 text

@CGuntur 44 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. See Appendix 1 for details

Slide 62

Slide 62 text

@CGuntur 44 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. 1 1 New object(s) added to Eden
 Age = 0 See Appendix 1 for details

Slide 63

Slide 63 text

@CGuntur 44 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. 1 2 1 New object(s) added to Eden
 Age = 0 2 First GC: Live objects moved
 From Eden —> Survivor 1 (S1)
 Age = 1 See Appendix 1 for details

Slide 64

Slide 64 text

@CGuntur 44 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. 1 2 3 1 New object(s) added to Eden
 Age = 0 2 First GC: Live objects moved
 From Eden —> Survivor 1 (S1)
 Age = 1 3 Second GC: Live objects moved
 From Eden, S1 —> S2
 Age ++ (E —> S1 = 1, S1 —> S2 = 2) See Appendix 1 for details

Slide 65

Slide 65 text

@CGuntur 44 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. 1 2 3 4 1 New object(s) added to Eden
 Age = 0 2 First GC: Live objects moved
 From Eden —> Survivor 1 (S1)
 Age = 1 3 Second GC: Live objects moved
 From Eden, S1 —> S2
 Age ++ (E —> S1 = 1, S1 —> S2 = 2) 4 Third GC: Live objects moved
 From Eden, S2 —> S1
 Age ++ (E —> S1 = 1, S2 —> S1 = 3) See Appendix 1 for details

Slide 66

Slide 66 text

@CGuntur 44 Generational GC Eden Survivor 1 Survivor 2 Tenured Permanent Gen. 1 2 3 4 n } 1 New object(s) added to Eden
 Age = 0 2 First GC: Live objects moved
 From Eden —> Survivor 1 (S1)
 Age = 1 3 Second GC: Live objects moved
 From Eden, S1 —> S2
 Age ++ (E —> S1 = 1, S1 —> S2 = 2) 4 Third GC: Live objects moved
 From Eden, S2 —> S1
 Age ++ (E —> S1 = 1, S2 —> S1 = 3) n Nth GC: Live objects moved
 From Current Survivor —> Tenured
 Age ~= 15 See Appendix 1 for details ...

Slide 67

Slide 67 text

@CGuntur Generational GC • Heap is broken into smaller areas (sub-heaps) • newly created objects (Age = 0)
 —> allocated to a young area: Eden • new objects that live beyond than a GC cycle (Age = 1)
 —> move to a young area: Survivor (S1) • objects that “survive” a few GC cycles (Age ++ )
 —> get moved between young areas: S1 -> S2 or S2 -> S1 • objects that survive longer (Age > 15)
 —> move to a an older area: Tenured • method definitions/statics etc
 —> allocated to PermGen, need not be collected 45

Slide 68

Slide 68 text

@CGuntur Collections for Young & Old Areas 46 Young Generation Eden, Survivors Old Generation
 Tenured Serial Collection Serial Serial Parallel Collection Parallel Scavenge Parallel Concurrent Mark-Sweep 
 Parallel New Same as Parallel New
 (same algorithm diff impl)
 Concurrent Mark Sweep until compaction time (Concurrent Mode Failure) then Parallel STW GC Type Generation See Appendix 2, 2a, 2b, 2c

Slide 69

Slide 69 text

@CGuntur 47 • Overview and history of garbage collection • Types of garbage collection • General patterns • Deeper dive into Generational GCs • The newer Garbage First GC • Recently added garbage collectors

Slide 70

Slide 70 text

@CGuntur Why a new GC? • Mostly focused on live objects not on garbage • Contiguous memory area arrangement reduces flexibility • Different collectors for different generations • By default, unpredictable and inconsistent pause-times • May need heavy tune-ups to stabilize pause times • Not performant for large memory heaps • Either highly prone to fragmentation 
 or demand smaller heaps for predictable times. 48 Issues with existing Generational GC

Slide 71

Slide 71 text

@CGuntur 49 Garbage First GC (G1GC) Eden Region Survivor Region Tenured Region Humongous Objects Free space

Slide 72

Slide 72 text

@CGuntur Eden Region Survivor Region Tenured Region Humongous Objects Free space 50 Garbage First GC (G1GC)

Slide 73

Slide 73 text

@CGuntur Garbage First GC (G1GC) Regions 51 See Appendix 3 for Region Math

Slide 74

Slide 74 text

@CGuntur Garbage First GC (G1GC) Regions • A region is a contiguous unit of memory 51 See Appendix 3 for Region Math

Slide 75

Slide 75 text

@CGuntur Garbage First GC (G1GC) Regions • A region is a contiguous unit of memory • Both for allocation and space reclamation 51 See Appendix 3 for Region Math

Slide 76

Slide 76 text

@CGuntur Garbage First GC (G1GC) Regions • A region is a contiguous unit of memory • Both for allocation and space reclamation • Regions are formed by dividing heap into 
 ~2048 or more equal size blocks 51 See Appendix 3 for Region Math

Slide 77

Slide 77 text

@CGuntur Garbage First GC (G1GC) Regions • A region is a contiguous unit of memory • Both for allocation and space reclamation • Regions are formed by dividing heap into 
 ~2048 or more equal size blocks • Region sizes are from 1 MB - 32 MB 
 (power of 2) based on the heap size 51 See Appendix 3 for Region Math

Slide 78

Slide 78 text

@CGuntur Garbage First GC (G1GC) Regions • A region is a contiguous unit of memory • Both for allocation and space reclamation • Regions are formed by dividing heap into 
 ~2048 or more equal size blocks • Region sizes are from 1 MB - 32 MB 
 (power of 2) based on the heap size • The memory manager assigns a region 51 See Appendix 3 for Region Math

Slide 79

Slide 79 text

@CGuntur Garbage First GC (G1GC) Regions • A region is a contiguous unit of memory • Both for allocation and space reclamation • Regions are formed by dividing heap into 
 ~2048 or more equal size blocks • Region sizes are from 1 MB - 32 MB 
 (power of 2) based on the heap size • The memory manager assigns a region • Regions are free, eden, survivor, tenured 51 See Appendix 3 for Region Math

Slide 80

Slide 80 text

@CGuntur Garbage First GC (G1GC) Regions • A region is a contiguous unit of memory • Both for allocation and space reclamation • Regions are formed by dividing heap into 
 ~2048 or more equal size blocks • Region sizes are from 1 MB - 32 MB 
 (power of 2) based on the heap size • The memory manager assigns a region • Regions are free, eden, survivor, tenured • Large objects allocated as a humongous region 51 See Appendix 3 for Region Math

Slide 81

Slide 81 text

@CGuntur Garbage First GC (G1GC) Regions • Humongous objects occupy complete regions • For larger objects, contiguous regions used • defined as objects ≥ 50% of region size. • not allocated as young regions • not collected in young gen. GC 52 See Appendix 3 for Region Math

Slide 82

Slide 82 text

@CGuntur 53 See Appendix 4 for Initial Mark G1GC Cycle

Slide 83

Slide 83 text

@CGuntur 54 See Appendix 5 for details See Appendix 6 for graphics Initial Mark 1. Triggers Root Scanning 2. Triggers Concurrent Marking 3. Continues the Young Collections Concurrent Marking 1. Works off a snapshot of the regions 2. Concurrent Process, not STW 3. Periodically halted by Young Collection Remark 1. Finalizes the marking 2. Finalizes Global Reference processing 3. Performs class-unloading 4. Calculated liveness information Cleanup 1. Updates internal structures 2. Reclaims completely empty regions 3. Determines a Space Reclamation need 4. Triggers a Young Collection Mixed Collection 1. Picks few Eden, Survivor and Tenured 2. Several cycles run, until heap waste low 3. Liveness of Tenured regions checked 4. Aim is to maintain consistent pause time Young Collection 1. Runs periodically to clear young regions 2. Only collection until below thresholds: • InitiatingHeapOccupancyPercent • G1ReservePercent G1GC Cycle

Slide 84

Slide 84 text

@CGuntur The old PermGen • PermGen allocated as a part of JVM Heap • PermGen is implicitly bounded • PermGen is allocated at startup. • PermGen could not use O/S memory swaps. • Default PermGen size is 64M 
 (85M for 64-bit scaled pointers). 55

Slide 85

Slide 85 text

@CGuntur The new Metaspace • Metaspaces are not a panacea for OutOfMemoryErrors. • Metaspace is explicitly bounded from the O/S memory, taking up unlimited amounts otherwise. • Initial Metaspace Size is set by -XX:MetaspaceSize 
 (replaces -XX:PermSize) • Max Metaspace is set by -XX:MaxMetaspaceSize 
 (replaces -XX:MaxPermSize) 56

Slide 86

Slide 86 text

@CGuntur 57 • Overview and history of garbage collection • Types of garbage collection • General patterns • Deeper dive into Generational GCs • The newer Garbage First GC • Recently added garbage collectors

Slide 87

Slide 87 text

@CGuntur Z GC • Region-based, non-generational concurrent collector, based on the G1GC • Designed for very large memory heaps and predictable throughput • Low GC pause times, not exceeding 10ms • No more than 15% application throughput reduction compared to using G1 • Aims at simplifying tuning of GCs as well • More reading material: • JEP-333 (https://openjdk.java.net/jeps/333) • ZGC Wiki (https://wiki.openjdk.java.net/display/zgc/Main) 58 (Oracle ⇢ OpenJDK) JDK 11

Slide 88

Slide 88 text

@CGuntur Epsilon GC • Completely passive GC with just bounded memory allocation and lowest latency guarantees • Linear allocation in a single chunk of memory but does not actually reclaim memory • Uses trivial lock-free Thread Local Allocation Buffers (TLABs) that do not need managing. • Best suited for: • performance testing the app (without GC latency). • extremely short lived jobs. • memory pressure testing. • More reading material: • JEP-318 (https://openjdk.java.net/jeps/318) • Remove the Garbage Collector (https://www.infoq.com/news/2017/03/java-epsilon-gc) 59 (Redhat ⇢ OpenJDK) JDK 11

Slide 89

Slide 89 text

@CGuntur Shenandoah GC • Region-based, non-generational collector, based on the G1GC • Young collection equivalent is run in a concurrent-partial mode • Uses a concurrent mark and a concurrent compact for longer lived objects • In slower collection, cycles are stolen from application, but is not STW • Pause times said to be independent of heap size (be it 2GB or even 100GB) • Best for responsiveness and predictable pauses than more cpu cycles and space • More reading material: • JEP-189 (http://openjdk.java.net/jeps/189) • Shenandoah Wiki (https://wiki.openjdk.java.net/display/shenandoah/Main) 60 (Redhat ⇢ OpenJDK) JDK 12

Slide 90

Slide 90 text

@CGuntur 61

Slide 91

Slide 91 text

@CGuntur Appendix 62

Slide 92

Slide 92 text

@CGuntur Appendix 63 Appendix Content Appendix 0 definitions for garbage collection jargon Appendix 1 details about how JDK v5 - v8 generational GC works Appendix 2 details about the combinations of collectors in JDK v5 - v8 generational GCs Appendix 3 how region sizes are calculated in G1GC Appendix 4 various triggers for Initial Mark in the G1GC Young Phase Appendix 5 detailed about the Young Phase of G1GC Appendix 6 graphical representation of the collection steps in G1GC Appendix 7 printing default values for the JVM options Appendix 8 logging the garbage collection using Unified Logging

Slide 93

Slide 93 text

@CGuntur Appendix 0 Throughput — the percentage of total time not spent in garbage collection, considered over long periods of time. Pause time — the length of time the application execution is stopped for garbage collection to occur. GC overhead — the inverse of throughput, that is, the percentage of total time spent in garbage collection. Collection frequency — how often collection occurs, relative to application execution. Footprint — a measure of size, such as heap size. Promptness — the time between when an object becomes garbage and when the memory becomes available 64 Garbage Collection - Definitions

Slide 94

Slide 94 text

@CGuntur Appendix 1 • there is a per-thread region for reducing synchronization 
 Thread Local Allocation Buffer (TLAB) • Eden itself divided into TLAB sections for • individual threads • a common area. • different GC processes for each generation • young region gc collectors typically are: • all-at-once • stop-the-world • copying pattern • old region gc typically concurrent 65 Generational GC - Details Back to Generational GC

Slide 95

Slide 95 text

@CGuntur Appendix 2 66 Generational GC Generational GC Young & Old GC Combinations Back to Collection Types

Slide 96

Slide 96 text

@CGuntur Appendix 2a Serial Collector • both young and old gen. are Serial • single thread, stop-the-world • designed for small heap sizes • useful for mobile or small apps 67 Generational GC - Combinations Back to Collection Types

Slide 97

Slide 97 text

@CGuntur Appendix 2b Parallel Collector • young gen. uses Parallel Scavenge • old gen. initially was a Serial • old gen. later improved to Parallel Old • default in JDK 1.5, 1.6, 1.7 and 1.8* 68 Generational GC - Combinations Back to Collection Types

Slide 98

Slide 98 text

@CGuntur Appendix 2c Concurrent Mark/Sweep (CMS) Collector • young gen. uses Serial or Parallel New • old gen. uses to Concurrent Mark Sweep • eventually old gen. compaction needed • old gen. compaction is Parallel Old 69 Generational GC - Combinations Back to Collection Types

Slide 99

Slide 99 text

@CGuntur Appendix 3 Assume a 9GB heap 1. 9GB = 9 x 1024MB = 9216 MB 2. Approximate number of regions (2048 or more) 3. 9216 MB / 2048 regions = 4.5 MB per region 4. Power of 2 less than 4.5 = 22 = 4MB
 Possible regions = 9216MB / 4MB = 2304 regions 5. Power of 2 more than 4.5 = 23 = 8MB
 Possible regions = 9216MB / 8MB = 1152 regions 6. Choosing: 2304 ≥ 2048, so region size 4MB is chosen 7. An object is thus considered humongous if size > 50% of region
 Humongous object size = 2MB or greater 70 G1GC - Region Math Back to G1GC Regions

Slide 100

Slide 100 text

@CGuntur Appendix 4 71 G1GC Young Collection What triggers Initial Mark? Back to G1GC Cycle

Slide 101

Slide 101 text

@CGuntur Appendix 4a Initiating Heap Occupancy Percent (IHOP) -XX:InitiatingHeapOccupancyPercent • Default value is 45, thus an Initial Mark is triggered when old gen heap size is 45% filled. • This is just an initiating value. G1 determines via measurement what the optimal percentage should be. • Such an adaptive HOP can be turned off by un-setting the flag (notice the -): -XX:-G1UseAdaptiveIHOP. • Turning off the Adaptive IHOP will make the G1 collector rely on the IHOP value alone. • This value is usually considered a soft threshold, reaching this limit may not immediately trigger Initial Mark. 72 G1GC Young Collection - Initial Mark - Triggers (1) Back to G1GC Cycle

Slide 102

Slide 102 text

@CGuntur Appendix 4b Guaranteed Survivor Space Availability Percent -XX:G1ReservePercent • Default value is 10, thus an Initial Mark is triggered when survivor space availability falls to 10% filled. • This is a flat unchanging value. G1 honors the value set during startup. • This value supersedes the Heap Occupancy Percentage triggers. • This value is usually considered a hard threshold, reaching this limit will immediately trigger Initial Mark. 73 G1GC Young Collection - Initial Mark - Triggers (2) Back to G1GC Cycle

Slide 103

Slide 103 text

@CGuntur Appendix 4c A Humongous allocation is reached • Humongous objects are objects with a size of 50% or greater than, a region size. • Directly allocated to Old gen. regions to avoid the potentially costly collections and moves of young gen. • G1GC tries to eagerly reclaim such objects if they are found to not have references after many collections. • Can be disabled by 
 -XX:-G1EagerReclaimHumongousObjects, may need to turn on Experimental options. 74 G1GC Young Collection - Initial Mark - Triggers (3) Back to G1GC Cycle

Slide 104

Slide 104 text

@CGuntur Appendix 5 •Root Scanning •scan local and static objects for root objects •mark in a "dirty queue" •Update Remembered Set (RSet) •all marked references in the dirty queue updated into a RSet •Process RSet •detect references to objects in the collection set •including objects in Tenured regions •Copy Live Objects •traverse the object graph and promote/age live objects •Process references •update references to new location •process soft, weak, phantom and final references 75 G1GC Young Collection - Steps Back to G1GC Cycle

Slide 105

Slide 105 text

@CGuntur Appendix 6 76 G1GC Young Collection - Details Back to G1GC Cycle

Slide 106

Slide 106 text

@CGuntur Appendix 6a 77 G1GC Young Collection - Eden + Survivor1 -> Survivor2 Back to G1GC Cycle E, SF —> ST Before After Collection Set Eden Region Survivor Region Tenured Region Humongous Objects Free space

Slide 107

Slide 107 text

@CGuntur Appendix 6b 78 Back to G1GC Cycle SF —> T Before After Collection Set G1GC Young Collection - Survivor -> Tenured Eden Region Survivor Region Tenured Region Humongous Objects Free space

Slide 108

Slide 108 text

@CGuntur Collection Set Appendix 6c 79 G1GC Young Collection - Initial Mark Back to G1GC Cycle Before After Eden Region Survivor Region Tenured Region Humongous Objects Free space

Slide 109

Slide 109 text

@CGuntur Appendix 6d 80 G1GC Young Collection - Concurrent Marking - Overview Back to G1GC Cycle Eden Region Survivor Region Tenured Region Humongous Objects Free space

Slide 110

Slide 110 text

@CGuntur Appendix 6e 81 G1GC Young Collection - Concurrent Marking - SATB 1. Snapshot-at-the-beginning (SATB) 2. Any changes after SATB 3. Root Scan 6. Completion 5. Children Painting 4. Root Painting Gray = To be evaluated, Black = Live, White = Dead Concurrent Marking - Snapshot-At-The-Beginning Back to G1GC Cycle New Object Added After SATB Object Dereferenced After SATB New Object Added After SATB Object Dereferenced After SATB New Object Added After SATB Object Dereferenced After SATB New Object Added After SATB Object Dereferenced After SATB New Object Added After SATB Object Dereferenced After SATB

Slide 111

Slide 111 text

@CGuntur Appendix 6f 82 G1GC Young Collection - Re-Mark Back to G1GC Cycle Collection Set Before After Eden Region Survivor Region Tenured Region Humongous Objects Free space Reference Outside
 Collection Set Remembered Set Add to RSet

Slide 112

Slide 112 text

@CGuntur Appendix 6g 83 G1GC Young Collection - Cleanup Back to G1GC Cycle Collection Set Before After Eden Region Survivor Region Tenured Region Humongous Objects Free space Remembered Set

Slide 113

Slide 113 text

@CGuntur Appendix 7 Print initial defaults for the operating system 
 (caution, this is a long list, best to redirect to a file) java -XX:+PrintFlagsInitial -version java -XX:+PrintFlagsInitial MyApplication Print final defaults with overridden values 
 (caution, this is a long list, best to redirect to a file) java -XX:+PrintFlagsFinal -version java -XX:+PrintFlagsFinal MyApplication Print current flags java -XX:+PrintCommandLineFlags -version java -XX:+PrintCommandLineFlags MyApplication 84 G1GC - Default values - Printing or or or The -version is used as the executable above. As is shown, it can be replaced with any java class with a main(...)

Slide 114

Slide 114 text

@CGuntur Appendix 8 85 G1GC Logging Back to G1GC Cycle

Slide 115

Slide 115 text

@CGuntur Appendix 8a 86 G1GC Logging - Unified logging in JDK 8+ Option Meaning -Xlog:gc Log messages with gc tag using info level to stdout, with default decorations -Xlog:gc,safepoint Log messages with either gc or safepoint tags (exclusive), both using info level, to stdout, with default decorations -Xlog:gc+ref=debug Log messages with both gc and ref tags, using debug level, to stdout, with default decorations -Xlog:gc=debug:file=gc.txt: none Log messages with gc tag using debug level to file gc.txt with no decorations -Xlog:gc=trace:file=gc.txt:
 uptimemillis, pids:filecount=5,filesize=1m Log messages with gc tag using trace level to a rotating logs of 5 files of size 1MB, using the base name gc.txt, with uptimemillis and pid decorations -Xlog:gc::uptime,tid Log messages with gc tag using info level to output stdout, using uptime and tid decorations -Xlog:gc*=info,
 safepoint*=off Log messages with at least gc using info level, but turn off logging of messages tagged with safepoint Unified logging changes (for reference use): java -Xlog:help Understanding the content in the table: -Xlog : [=] [: [: ]] Reference: https://www.slideshare.net/PoonamBajaj5/lets-learn-to-talk-to-gc-logs-in-java-9

Slide 116

Slide 116 text

@CGuntur Appendix 8b 87 G1GC Logging - Tags commonly used Tag Type Tag Region region Liveness liveness Marking marking Remembered Set remset Ergonomics ergo Class Histogram classhisto Safepoint safepoint Task task Heap heap JNI jni Promotion Local Allocation Buffer plab Tag Type Tag Promotion promotion Reference ref String Deduplication stringdedup Statistics stats Tenuring age Thread Local Allocation Buffer tlab Metaspace metaspace Humongous Allocation alloc Refinement refine Humongous humongous String Symbol Table stringtable Main tag is gc