Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java Garbage Collection - The Basics

Chandra Guntur
September 02, 2020

Java Garbage Collection - The Basics

Presented at GIDS.Java 2020, online.

Modern Java has a collection of sophisticated Garbage Collection patterns and models.

This session will provide an overview of how basic Garbage Collection patterns and models work in Java. The presentation material includes various Regional Collector JVM options and flags for tuning GC behavior as well as new logging techniques using Java Unified Logging.

Details will be shared about the new garbage collectors being introduced since Java 10, covering Shenandoah, ZGC and Epsilon.

Chandra Guntur

September 02, 2020
Tweet

More Decks by Chandra Guntur

Other Decks in Technology

Transcript

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

    View full-size slide

  2. @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’)

    View full-size slide

  3. @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

    View full-size slide

  4. @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

    View full-size slide

  5. @CGuntur
    What is Garbage Collection?
    5

    View full-size slide

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

    View full-size slide

  7. @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

    View full-size slide

  8. @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

    View full-size slide

  9. @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

    View full-size slide

  10. @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

    View full-size slide

  11. @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

    View full-size slide

  12. @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

    View full-size slide

  13. @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

    View full-size slide

  14. @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

    View full-size slide

  15. @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

    View full-size slide

  16. @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

    View full-size slide

  17. @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

    View full-size slide

  18. @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

    View full-size slide

  19. @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

    View full-size slide

  20. @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

    View full-size slide

  21. @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

    View full-size slide

  22. @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

    View full-size slide

  23. @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

    View full-size slide

  24. @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

    View full-size slide

  25. @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

    View full-size slide

  26. @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

    View full-size slide

  27. @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

    View full-size slide

  28. @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

    View full-size slide

  29. @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

    View full-size slide

  30. @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

    View full-size slide

  31. @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

    View full-size slide

  32. @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

    View full-size slide

  33. @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

    View full-size slide

  34. @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

    View full-size slide

  35. @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

    View full-size slide

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

    View full-size slide

  37. @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

    View full-size slide

  38. @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

    View full-size slide

  39. @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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  44. @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

    View full-size slide

  45. @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

    View full-size slide

  46. @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

    View full-size slide

  47. @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

    View full-size slide

  48. @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

    View full-size slide

  49. @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

    View full-size slide

  50. @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

    View full-size slide

  51. @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

    View full-size slide

  52. @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

    View full-size slide

  53. @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

    View full-size slide

  54. @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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  57. @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

    View full-size slide

  58. @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

    View full-size slide

  59. @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

    View full-size slide

  60. @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

    View full-size slide

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

    View full-size slide

  62. @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

    View full-size slide

  63. @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

    View full-size slide

  64. @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

    View full-size slide

  65. @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

    View full-size slide

  66. @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
    ...

    View full-size slide

  67. @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

    View full-size slide

  68. @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

    View full-size slide

  69. @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

    View full-size slide

  70. @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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  75. @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

    View full-size slide

  76. @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

    View full-size slide

  77. @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

    View full-size slide

  78. @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

    View full-size slide

  79. @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

    View full-size slide

  80. @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

    View full-size slide

  81. @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

    View full-size slide

  82. @CGuntur 53
    See Appendix 4 for Initial Mark
    G1GC Cycle

    View full-size slide

  83. @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

    View full-size slide

  84. @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

    View full-size slide

  85. @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

    View full-size slide

  86. @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

    View full-size slide

  87. @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

    View full-size slide

  88. @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

    View full-size slide

  89. @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

    View full-size slide

  90. @CGuntur
    Appendix
    62

    View full-size slide

  91. @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

    View full-size slide

  92. @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

    View full-size slide

  93. @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

    View full-size slide

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

    View full-size slide

  95. @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

    View full-size slide

  96. @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

    View full-size slide

  97. @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

    View full-size slide

  98. @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

    View full-size slide

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

    View full-size slide

  100. @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

    View full-size slide

  101. @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

    View full-size slide

  102. @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

    View full-size slide

  103. @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

    View full-size slide

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

    View full-size slide

  105. @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

    View full-size slide

  106. @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

    View full-size slide

  107. @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

    View full-size slide

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

    View full-size slide

  109. @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

    View full-size slide

  110. @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

    View full-size slide

  111. @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

    View full-size slide

  112. @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(...)

    View full-size slide

  113. @CGuntur
    Appendix 8
    85
    G1GC Logging
    Back to G1GC Cycle

    View full-size slide

  114. @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

    View full-size slide

  115. @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

    View full-size slide