Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Performance Engineer's Guide To (OpenJDK) HotSpot Garbage Collection - The Facts, The Algorithm & Tuning.

1,617 views

Published on

Monica Beckwith is a JVM/GC Performance Engineer and has worked with the HotSpot VM for more than a decade. In this presentation, she will talk about GC Performance Engineering while providing GC facts and examples. She will also discuss various OpenJDK HotSpot GC algorithms and will provide advice on this topic.

Published in: Technology
  • Be the first to comment

The Performance Engineer's Guide To (OpenJDK) HotSpot Garbage Collection - The Facts, The Algorithm & Tuning.

  1. 1. By: Monica Beckwith Code Karam LLC
  2. 2. 2 About Me … Java/JVM/GC performance person Current Life: Consultant who solves Managed Runtime performance issues Past Life: Worked with Oracle, Sun, AMD… - Was the Performance Lead for G1 GC www.codekaram.com https://www.linkedin.com/in/monicabeckwith Tweet @mon_beck
  3. 3. ©2016 CodeKaram The Facts • Performance Engineering • JVM • GC • Throughput and Latency Design Considerations • Generational GC 3
  4. 4. ©2016 CodeKaram The Algorithm • Allocations • Thread Local Allocation Buffers (TLABs) • Reclamation • Young Collection • Old Collection • Parallel and Serial Mark-Compact • Mostly Concurrent Mark-Sweep 4
  5. 5. ©2016 CodeKaram Tuning • Memory Leaks • Tuning Recommendations • Tuning for Throughput • Tuning for Latency 5
  6. 6. ©2016 CodeKaram Extras • Garbage First Garbage Collector 6
  7. 7. 7 Performance Engineering
  8. 8. ©2016 CodeKaram8 Performance Engineering Performance Requirements & Test Plan Development Performance Analysis
  9. 9. 9 Performance Requirements
  10. 10. ©2016 CodeKaram10 Performance Requirements/ SLAs Throughput? Response times? Capacity Management? Footprint? Availability?
  11. 11. 11 Performance Analysis
  12. 12. ©2016 CodeKaram12 Performance Analysis Monitoring System Under Test Utilization, Lock Stats, … Application, System, Memory, … Profiling Analyzing logs
  13. 13. 13 JVM Performance Engineering
  14. 14. ©2016 CodeKaram14 Java Application Java APIs Java VM OS + HardwareJRE Runtime JIT Compiler GC Class loader
  15. 15. ©2016 CodeKaram15 Study, Analyze, TuneJIT Compiler + Runtime Java Development Kit Garbage Collector/ Collection Java APIs
  16. 16. 16 Garbage Collection Performance Engineering
  17. 17. ©2016 CodeKaram17 GC Performance Engineering JVM Monitoring Application Profiling Process & Plot GC logs Online & Offline GC Monitoring Heap Profiling
  18. 18. 18 OpenJDK HotSpot GC - Facts!
  19. 19. ©2016 CodeKaram Throughput and latency are the two main drivers towards refinement of GC algorithms. 19 GC Fact #1
  20. 20. ©2016 CodeKaram The Throughput Maximizer 20 Maximize Throughput Generational Old Parallel Work Young Concurrent Threads Stop-the- world Threads
  21. 21. ©2016 CodeKaram Mr. Latency Sensitive 21 Latency Sensitive Pause only when needed Concurrent Marking/Sweeping Reduce stop-the- world GC pause time No/Partial Compaction 21
  22. 22. ©2016 CodeKaram22 Throughput Maximizer Parallel GC
  23. 23. ©2016 CodeKaram23 Latency Sensitive CMS GC
  24. 24. ©2016 CodeKaram24 Throughput Maximizer Latency Sensitive Parallel GC CMS GC G1 GC
  25. 25. ©2016 CodeKaram All GCs in OpenJDK HotSpot are generational. 25 GC Fact #2
  26. 26. ©2016 CodeKaram26 Young Generation Old Generation Eden Survivors
  27. 27. 27 OpenJDK HotSpot GC - Algorithms!
  28. 28. 28 Garbage Collection - Allocation.
  29. 29. ©2016 CodeKaram29 Eden Allocations Survivors Young Generation
  30. 30. 30 Fast Path Allocation == Lock-Free Allocation == Threads Allocate Into Their Local Allocation Buffer (LAB)s
  31. 31. ©2016 CodeKaram31 Eden TLAB TLAB TLAB TLAB TLAB TLAB = Thread Local Allocation Buffer
  32. 32. ©2016 CodeKaram32 EdenThread 1 Thread 2 Thread 3 Thread 4 TLAB TLAB TLAB TLAB TLAB Thread 0
  33. 33. ©2016 CodeKaram33 Eden Allocations Survivors Young Generation S0 S1
  34. 34. ©2016 CodeKaram34 Eden Allocations Survivors Young Generation S0 S1
  35. 35. 35 Garbage Collection - Reclamation.
  36. 36. ©2016 CodeKaram36 Young Generation Old Generation Eden S0 S1
  37. 37. ©2016 CodeKaram37 *Similar GC Algorithms for OpenJDK HotSpot Different GC Algorithms for OpenJDK Hotspot Always collected in its entirety Young Generation Old Generation
  38. 38. ©2016 CodeKaram38 Always collected in its entirety Young Generation Old Generation *Similar GC Algorithms for OpenJDK HotSpot Different GC Algorithms for OpenJDK Hotspot
  39. 39. 39 Young Garbage Collection == Reclamation Via Scavenging
  40. 40. ©2016 CodeKaram40 Old Generation Root Set Young Generation
  41. 41. ©2016 CodeKaram41 SurvivorEden Young Generation
  42. 42. ©2016 CodeKaram42 SurvivorEden Young Generation
  43. 43. ©2016 CodeKaram43 SurvivorEden Young Generation
  44. 44. 44 Old Garbage Collection == Different GC Algorithms
  45. 45. ©2016 CodeKaram The Throughput Collector Multi-Threaded Multi-Threaded - Entire Heap Marked, Swept and Compacted in its entirety Always collected in its entirety 45 Young Generation Old Generation
  46. 46. ©2016 CodeKaram46 Multi-Threaded Multi-Threaded - Entire Heap Marked, Swept and Compacted in its entirety Always collected in its entirety Young Generation Old Generation
  47. 47. ©2016 CodeKaram47 Old Generation Root Set Young Generation Garbage Collection -Reclamation via Parallel Mark-Compact
  48. 48. ©2016 CodeKaram Garbage Collection -Reclamation via Parallel Mark-Compact 48 Old Generation
  49. 49. ©2016 CodeKaram49 Garbage Collection -Reclamation via Parallel Mark-Compact Old Generation
  50. 50. ©2016 CodeKaram50 Garbage Collection -Reclamation via Parallel Mark-Compact Old Generation source region destination region
  51. 51. ©2016 CodeKaram51 Garbage Collection -Reclamation via Parallel Mark-Compact Old Generation source region destination region
  52. 52. ©2016 CodeKaram52 Garbage Collection -Reclamation via Parallel Mark-Compact Old Generation source region destination region source region
  53. 53. ©2016 CodeKaram53 Garbage Collection -Reclamation via Parallel Mark-Compact Old Generation source region destination region source region
  54. 54. ©2016 CodeKaram54 Garbage Collection -Reclamation via Parallel Mark-Compact Old Generation
  55. 55. ©2016 CodeKaram The CMS Collector Multi-Threaded Multi-Threaded - Mostly Concurrent Marked and Swept in its entirety Always collected in its entirety 55 Young Generation Old Generation
  56. 56. ©2016 CodeKaram The CMS Collector Multi-Threaded Multi-Threaded - Mostly Concurrent Marked and Swept in its entirety Always collected in its entirety 56 ?? Young Generation Old Generation
  57. 57. ©2016 CodeKaram The CMS Collector Multi-Threaded Multi-Threaded - Mostly Concurrent Marked and Swept in its entirety Always collected in its entirety 57 No Compaction!! Young Generation Old Generation
  58. 58. ©2016 CodeKaram Garbage Collection - Reclamation via Mark-Sweep 58 Old Generation Free List
  59. 59. ©2016 CodeKaram Garbage Collection - Reclamation via Mark-Sweep 59 Old Generation Free List
  60. 60. ©2016 CodeKaram All non/partial compacting GCs in OpenJDK HotSpot fallback* to a fully compacting stop-the-world garbage collection called the “full” GC. *Tuning can help avoid or postpone full GCs in many cases. 60
  61. 61. ©2016 CodeKaram Single Threaded - Entire Heap Marked, Swept and Compacted in its entirety 61 Young Generation Old Generation Garbage Collection -Reclamation via Mark-Sweep-Compact
  62. 62. ©2016 CodeKaram Single Threaded - Entire Heap Marked, Swept and Compacted in its entirety 62 Young Generation Old Generation Garbage Collection -Reclamation via Mark-Sweep-Compact
  63. 63. 63 OpenJDK HotSpot GC - Tuning!
  64. 64. ©2016 CodeKaram • GC can NOT eliminate your memory leaks! • GC (and heap dump) can provide an insight into your application. 64 About Memory Leaks
  65. 65. ©2016 CodeKaram Java and Memory Leak 65 • Automatic Memory Management • Allocation - mostly lock-free out of TLABs • Reclamation - after tracing live data tree referenced from the root set. • How can there be a leak? …. hint above :)
  66. 66. ©2016 CodeKaram Java and Memory Leak 66 • GC root • class loaded by class loader • static fields • threads • JNI local and global
  67. 67. ©2016 CodeKaram Java and Memory Leak 67 • class loader leaks • mutable static fields • ThreadLocal leaks • JNI memory leaks
  68. 68. ©2016 CodeKaram Java and Memory Leak 68 • Tools • GC log files • Heap dump • Heap profiling Live objects retention information
  69. 69. ©2016 CodeKaram Java and Memory Leak 69 • Tools • -XX:+PrintGCDetails -XX: +PrintGCTimeStamps - Xloggc:filename; parse; plot • jvisualVM; jconsole; Java Mission Control
  70. 70. ©2016 CodeKaram70 Total Occupied Heap Old generation Young generation Live Data Set
  71. 71. ©2016 CodeKaram 71
  72. 72. ©2016 CodeKaram 72
  73. 73. 73 Tuning GC For Throughput Or For Latency??
  74. 74. 74 GC Elapsed Time Or GC Overhead
  75. 75. ©2016 CodeKaram GC Elapsed Time indicated the amount of time it takes to execute stop the world GC events The higher the GC elapsed time - the lower the application responsiveness due to the GC induced latencies 75
  76. 76. ©2016 CodeKaram Overhead is an indication of the frequency of stop the world GC events. The more frequent the GC events - The more likely it is to negatively impact application throughput 76
  77. 77. 77 What’s The #1 Contributor To A GC Elapsed Time?
  78. 78. Copying Costs! 78
  79. 79. 79 What Are The ContributorS To GC Overhead?
  80. 80. 80 Allocation Rate and Promotion Rate
  81. 81. ©2016 CodeKaram • Size generations keeping your application’s object longevity and size in mind. • The faster the generation gets “filled”; the sooner a GC is triggered. • Size your generations and age your objects appropriately. • The higher the amount of live data to be copied, the longer the GC pause. • Premature promotions are a big problem! 81 Tuning Recommendations
  82. 82. ©2016 CodeKaram Plot Allocation & Promotion Rates 82
  83. 83. ©2016 CodeKaram83 Young Occupancy before GC Young Gen Size Old Gen Occupancy after GC Heap Occupancy before GC Heap Occupancy after GC Heap Size Timestamps
  84. 84. 84 Time Check!!
  85. 85. 85 What About Garbage First GC?
  86. 86. ©2016 CodeKaram Traditional Java Heap 86 Contiguous Java Heap Eden S0 S1 Old Generation
  87. 87. ©2016 CodeKaram Heap Regions 87 Contiguous Java Heap Free Region Non-Free Region
  88. 88. ©2016 CodeKaram The Garbage First Collector - Regionalized Heap Eden Old Old Eden Old Survivor Humongous 88
  89. 89. ©2016 CodeKaram The Garbage First Collector Eden Old Old Eden Old Surv ivor E.g.: Current heap configuration - 89
  90. 90. ©2016 CodeKaram The Garbage First Collector Eden Old Old Eden Old Surv ivor E.g.: During a young collection - 90
  91. 91. ©2016 CodeKaram The Garbage First Collector Old Old Old E.g.: After a young collection - Sur viv or Ol d 91
  92. 92. ©2016 CodeKaram Old Old Old Sur viv or Ol d Eden Eden Old The Garbage First Collector 92 E.g.: Current heap configuration -
  93. 93. ©2016 CodeKaram Old Old Old E.g.: Reclamation of a garbage-filled region during the cleanup phase - Sur viv or Ol d Eden Eden Old The Garbage First Collector 93
  94. 94. ©2016 CodeKaram Old Old Old E.g.: Reclamation of a garbage-filled region during the cleanup phase - Sur viv or Ol d Eden Eden The Garbage First Collector 94
  95. 95. ©2016 CodeKaram Old Old Old E.g.: Current heap configuration - Sur viv or Ol d Eden Eden The Garbage First Collector 95
  96. 96. ©2016 CodeKaram Old Old Old E.g.: During a mixed collection - Sur viv or Ol d Eden Eden The Garbage First Collector 96
  97. 97. ©2016 CodeKaram Old E.g.: After a mixed collection - O l d Sur vivo r Old The Garbage First Collector 97
  98. 98. ©2016 CodeKaram The Garbage First Collector - Pause Histogram 98 Pausetimeinmilliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4 3432.2 3433.2 3436.8 3437.6 3438.9 3440 Young Collection Initial Mark Remark Cleanup Mixed Collection Initiating Heap Occupancy Percent
  99. 99. ©2016 CodeKaram Questions? 99 hotspot-gc-use@openjdk.java.net hotspot-gc-dev@openjdk.java.net monica@codekaram.com www.codekaram.com

×