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

Concurrent GCs - ZGC & Shenandoah

Simone Bordet
September 22, 2019

Concurrent GCs - ZGC & Shenandoah

Java 11 & Java 12 offer two new Garbage Collectors: ZGC and Shenandoah.

The goal of these new concurrent collectors is to reduce GC pauses to only few milliseconds and possibly sub-milliseconds, solving once for all the GC problem.

Simone Bordet

September 22, 2019
Tweet

More Decks by Simone Bordet

Other Decks in Programming

Transcript

  1. [email protected] Simone Bordet • @simonebordet • [email protected] • Works @

    Webtide ◦ The company behind Jetty and CometD • JVM Tuning Expert 2
  2. [email protected] Introduction Copy Young Generation Old Generation Parallel Mark Compact

    Copy CMS Conc Mark Conc Sweep Stop-The-World Concurrent 5
  3. [email protected] Introduction Copy Young Generation Old Generation Parallel Mark Compact

    Copy CMS Conc Mark Conc Sweep Copy G1 Conc Mark Compact Stop-The-World Concurrent 6
  4. [email protected] Introduction Copy Young Generation Old Generation Parallel Mark Compact

    Copy CMS Conc Mark Conc Sweep Copy G1 Conc Mark Compact ZGC Conc Mark Conc Compact Shenandoah Conc Mark Conc Compact Stop-The-World Concurrent 7
  5. [email protected] Introduction • Concurrent GCs ◦ GC runs concurrently with

    the application • GC races with Application ◦ Marking an object “alive” ◦ Compacting / Moving objects 8
  6. [email protected] Introduction 11 Header a = 1 Header f Header

    a = 1 Region Region Region "From Space" "To Space"
  7. [email protected] Introduction • Garbage Collector updates references 13 Header a

    = 3 Header f Header a = 1 Region Region Region LOST WRITE!
  8. [email protected] Introduction • Concurrent GCs store object metadata ◦ E.g.

    whether an object has been marked • Concurrent GCs require JIT support • JIT injects code that helps the GC ◦ GC barriers 14
  9. [email protected] ZGC • Present in OpenJDK 11+ ◦ Available in

    AdoptOpenJDK builds ◦ Available in Oracle OpenJDK builds ▪ From https://jdk.java.net • Scalable Low Latency GC • Concurrent Compaction, Single Generation 16
  10. [email protected] ZGC • Only for Linux x86 64-bit ◦ No

    compressed pointers ◦ ARM port underway • Region Based ◦ ZPages - similar to G1 regions ▪ Small (2 MiB - object size up to 256 KiB) ▪ Medium (32 MiB - object size up to 4 MiB) ▪ Large (4+ MiB - object size > 4 MiB) 17
  11. [email protected] ZGC GC Thread App Thread 21 Weak Reference Processing

    Free Memory Pages Unload Classes Prepare Relocation Set Concurrent Processing
  12. [email protected] ZGC • ZGC solution ◦ To avoid races with

    the application • Colored Pointers • Load GC barrier 25
  13. [email protected] ZGC • ZGC colored pointers ◦ Color indicates GC

    metadata • Check pointer color against GC phase ◦ Wrong color => take action Object address - 44 bits (16 TiB) Unused - 16 bits 64 bits pointer address 4 color bits 26
  14. [email protected] ZGC • ZGC load GC barrier Object f =

    obj.field; if (addr_of(f) & wrong_gc_color) { slow_path() } 28
  15. [email protected] ZGC • ZGC load GC barrier Object f =

    obj.field; <load_barrier> mov 0x10(%rdi), %rsi test %rsi, 0x20(%r15) jne slow_path 29
  16. [email protected] ZGC • ZGC load GC barrier tests for the

    right color • Wrong color -> take slow_path ◦ Fix color & Run some action - atomically • The action depends on the GC phase 30
  17. [email protected] ZGC • ZGC Multi-Mapping • RSS shows 3x size

    ◦ E.g. -Xmx=16G -> RSS~=50G ◦ On Linux, use smem and track PSS • Watch out for OOM Killers in clouds ◦ E.g. AWS 32
  18. [email protected] • Relocation ZGC A E B D F C

    C’ D’ MetaData B -> B’ C -> C’ D -> D’ 34 B’
  19. [email protected] • Relocation ZGC A E B D F C

    B’ C’ D’ E’ Load Barrier MetaData B -> B’ C -> C’ D -> D’ E -> E’ 35
  20. [email protected] • Relocation ZGC A E B D F C

    B’ C’ D’ E’ E” MetaData B -> B’ C -> C’ D -> D’ E -> E’ 36
  21. [email protected] • Relocation ZGC A E B D F C

    B’ C’ D’ E’ E” MetaData B -> B’ C -> C’ D -> D’ E -> E’ 37 E -> E”
  22. [email protected] • Relocation ZGC A E B D F C

    B’ C’ D’ E’ F’ MetaData B -> B’ C -> C’ D -> D’ E -> E’ F -> F’ 38
  23. [email protected] ZGC • References ◦ Per Lidén, Erik Österlund et

    al. ▪ https://www.youtube.com/watch?v=7cWiwu7kYkE ▪ https://www.youtube.com/watch?v=kF_r3GE3zOo ◦ Project Wiki ▪ https://wiki.openjdk.java.net/display/zgc/Main ◦ Mailing List ▪ [email protected] 40
  24. [email protected] ShenandoahGC • Present in OpenJDK 12+ repository ◦ Available

    in AdoptOpenJDK builds ◦ NOT available in Oracle’s OpenJDK builds ◦ Present in RedHat OpenJDK 8, 11+ • Scalable low latency GC • Concurrent Compaction, Single Generation 42
  25. [email protected] ShenandoahGC • Available for x86 32-bit and 64-bit ◦

    ARM ports available • Linux, MacOS, Windows ◦ JDK 8, 11 & Latest • Region Based ◦ Derived from G1 43
  26. [email protected] ShenandoahGC GC Thread App Thread 46 Weak Reference Processing

    Unload Classes Evacuate Roots Housekeeping STW Mark End
  27. [email protected] ShenandoahGC • ShenandoahGC solution ◦ To avoid races with

    the application • Brooks (Forward) Pointers • Load & Store GC Barriers 49
  28. [email protected] ShenandoahGC • HotSpot Object Header ◦ "Word" == 32

    bits on i386; 64 bits on x86-64 Mark Word Class Word Field 1 Field 2 Mark Word Class Word Array Length Slot 1 Slot 2 50
  29. [email protected] ShenandoahGC 1.0 • ShenandoahGC 1.0 Class Field1 Field2 Class

    Field1 Field2 Mark Field1 Field2 Mark Field1 Field2 Fwd Ptr Fwd Ptr Mark Mark Class Class 52
  30. [email protected] ShenandoahGC 1.0 • ShenandoahGC 1.0 • Maintains weak to-space

    invariant ◦ Reads from both from-space to-space copies ◦ Writes only to to-space copy 53
  31. [email protected] ShenandoahGC 1.0 • Load GC barrier: dereference forward pointer

    Object f = obj.field; <load_barrier> mov 0x10(%rdi), %rsi mov -0x08(%rsi), %rsi 56
  32. [email protected] ShenandoahGC • Store GC barrier ◦ Runs some action

    depending on GC phase obj.f = o; <store_barrier> 57
  33. [email protected] ShenandoahGC • Store GC barrier ◦ Runs some action

    depending on GC phase obj.f = o; if (in_evac_phase && in_collection_set(obj) && !is_forwarded(obj)) slow_path(); 58
  34. [email protected] ShenandoahGC • Store GC barrier ◦ Runs some action

    depending on GC phase obj.f = o; mov 0x3d8(%r15), %r11 test %r11, %r11 ... jne <slow_path> 59
  35. [email protected] ShenandoahGC • ShenandoahGC 1.0 Problems • More memory needed

    (due to the forward pointer) ◦ Worst case 50%, common case 5-10% • Complicated (exotic) GC barriers ◦ Load & Store GC barriers to maintain invariants 60
  36. [email protected] ShenandoahGC • ShenandoahGC 2.0 Class Field1 Field2 Class Field1

    Field2 Mark Field1 Field2 Field1 Field2 Fwd Ptr Mark Mark Class Class Mark Field1 Field2 Class From Space To Space 62
  37. [email protected] ShenandoahGC • ShenandoahGC 2.0 • Maintains a strong to-space

    invariant ◦ Reads only possible from to-space copy ◦ Writes only possible from to-space copy 63
  38. [email protected] ShenandoahGC 2.0 • Load Barrier (LRB - Load Reference

    Barrier) ◦ Checks whether there may be forwarded objects ▪ And whether in c-set and whether is forwarded Object f = obj.f; <load_barrier> 64
  39. [email protected] ShenandoahGC 2.0 • Load Barrier (LRB - Load Reference

    Barrier) Object f = obj.f; if (in_evac_phase && in_collection_set(obj) && !is_forwarded(obj)) slow_path(); 65
  40. [email protected] ShenandoahGC 2.0 • Load Barrier (LRB - Load Reference

    Barrier) Object f = obj.f; test $0x1,0x20(%r15) ... jne <slow_path> 66
  41. [email protected] ShenandoahGC 2.0 • Shenandoah 2.0 • Load and Store

    GC barrier are the same! ◦ Big codebase simplification ◦ Improved performance 67
  42. [email protected] • Evacuation (Application triggers LRB) ShenandoahGC Class Field1 Field2

    Mark Class Field1 Field2 Mark Class Field1 Field2 Mark To Space From Space 70
  43. [email protected] • Evacuation (Atomic Fwd Ptr Update) ShenandoahGC Class Field1

    Field2 Mark Class Field1 Field2 Mark Class Field1 Field2 Mark To Space From Space 71
  44. [email protected] • Evacuation (Application Wins) ShenandoahGC Class Field1 Field2 Fwd

    Ptr Class Field1 Field2 Mark Class Field1 Field2 Mark To Space From Space 72
  45. [email protected] ShenandoahGC • Shenandoah Heuristics ◦ Adaptive (default) ▪ Maintains

    free heap amount ◦ Static ▪ Starts GC when tripping thresholds ◦ Compact ▪ Small footprint ◦ Aggressive ▪ Back to back GCs ◦ Passive ▪ Non concurrent, only Full GCs ◦ Traversal 75
  46. [email protected] ShenandoahGC • References ◦ Project Wiki ▪ https://wiki.openjdk.java.net/display/shenandoah/Main ◦

    Aleksey Shipilëv, Roman Kennke et al. ▪ https://www.youtube.com/watch?v=VCeHkcwfF9Q ▪ https://www.youtube.com/watch?v=E1M3hNlhQCg ◦ Mailing List ▪ [email protected] 76
  47. [email protected] Conclusions • Give Concurrent GCs a go • Stuck

    with JDK 8? ◦ Use Shenandoah Already using JDK 11+? ◦ ZGC or Shenandoah • Report your feedback! 79