106
A Song of JIT and GC 1 QCon London 2016 Monica Beckwith [email protected]; @mon_beck https://www.linkedin.com/in/monicabeckwith www.codekaram.com Game of Performance

A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

A Song of JIT and GC

1

QCon London 2016

Monica Beckwith [email protected]; @mon_beck

https://www.linkedin.com/in/monicabeckwith www.codekaram.com

Game of Performance

Page 2: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

About Me

• Java/JVM/GC Performance Engineer/Consultant

• Worked at AMD, Sun, Oracle…

• Worked with HotSpot JVM for more than a decade

• JVM heuristics, JIT compiler, GCs: Parallel(Old) GC, G1 GC, CMS GC

2

Page 3: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Many Thanks

• Vladimir Kozlov (Hotspot JIT Team) - for clearing my understanding of tiered compilation, escape analysis and nuances of dynamic deoptimizations.

• Jon Masamitsu (Hotspot GC Team) - for keeping me on track with JDK8 changes that related to GC.

3

Page 4: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Agenda• The Helpers within the JVM

• JIT & Runtime

• Adaptive Optimization

• CompileThreshold

• Inlining

• Dynamic Deoptimization4

Page 5: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Agenda• The Helpers within the JVM

• JIT & Runtime

• Tiered Compilation

• Intrinsics

• Escape Analysis

• Compressed Oops

• Compressed Class Pointers

5

Page 6: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Agenda

• The Helpers within the JVM

• Garbage Collection

• Allocation

• TLAB

• NUMA Aware Allocator

6

Page 7: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Agenda• The Helpers within the JVM

• Garbage Collection

• Reclamation

• Mark-Sweep-Compact (Serial + Parallel)

• Mark-Sweep

• Scavenging7

Page 8: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Agenda

• The Helpers within the JVM

• Garbage Collection

• String Interning and Deduplication

8

Page 9: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Interfacing The Metal

9

Java Application

Java APIs

RuntimeJIT Compiler

GC

Java VM

Class loader

OS + Hardware

Page 10: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Interfacing The Metal

10

Java Application

Java APIs

Java VM

OS + HardwareJRE

RuntimeJIT Compiler

GC

Class loader

Page 11: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

The Helpers

11

Java VM

RuntimeJIT Compiler

GC

Page 12: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

12

The Helpers - JIT And Runtime.

Page 13: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

13

Advanced JIT & Runtime Optimizations - Adaptive

Optimization

Page 14: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• Startup - Interpreter

• Adaptive optimization - Performance critical methods

• Compilation:

• CompileThreshold

• Identify root of compilation

• Method Compilation or On-stack replacement (Loop)?

14

JIT & Runtime

Page 15: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

PrintCompilation

15

timestamp compilation-id flags tiered-compilation-level Method <@ osr_bci> code-size <deoptimization>

Page 16: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

PrintCompilation

16

Flags:%: is_osr_methods: is_synchronized!: has_exception_handlerb: is_blockingn: is_native

Page 17: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

PrintCompilation

17

567 693 % ! 3 org.h2.command.dml.Insert::insertRows @ 76 (513 bytes)

656 797 n 0 java.lang.Object::clone (native)

779 835 s 4 java.lang.StringBuffer::append (13 bytes)

Page 18: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• Adaptive optimization - Performance critical methods

• Inlining:

• MinInliningThreshold, MaxFreqInlineSize, InlineSmallCode, MaxInlineSize, MaxInlineLevel, DesiredMethodLimit …

18

JIT & Runtime

Page 19: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

PrintInling*

19

@ 76 java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot)@ 91 java.lang.System::arraycopy (0 bytes) (intrinsic)@ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large

* needs -XX:+UnlockDiagnosticVMOptions

Page 20: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• Adaptive optimization - Performance critical methods

• Dynamic de-optimization

• dependencies invalidation

• classes unloading and redefinition

• uncommon path in compiled code

20

JIT & Runtime

Page 21: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

PrintCompilation

21

573 704 2 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie

Page 22: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

22

Advanced JIT & Runtime Optimizations - Tiered

Compilation

Page 23: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• Start in interpreter

• Tiered optimization with client compiler

• Code profiled information

• Enable server compiler

23

Tiered Compilation

Page 24: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• C1 compilation threshold for tiered is about a 100 invocations

• Tiered Compilation has a lot more profiled information for C1 compiled methods

• CodeCache needs to be 5x larger than non-tiered

• Default on JDK8 when tiered is enabled (48MB vs 240MB)

• Need more? Use -XX:ReservedCodeCacheSize

24

Tiered Compilation - Effect on Code Cache

Page 25: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• Fast dynamic type tests for type safety

• Range check elimination

• Loop unrolling

• Profile data guided optimizations

• Escape Analysis

• Intrinsics

• Vectorization

25

Other JIT Compiler Optimizations

Page 26: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

26

Advanced JIT & Runtime Optimizations - Intrinsics

Page 27: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram27

Without IntrinsicsJava Method

JIT Compilation

Execute Generated Code

Page 28: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram28

IntrinsicsJava Method

JIT Compilation

Execute Optimized Code

Call Hand-Optimized

Assembly Code

Page 29: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

PrintInling*

29

@ 76 java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot)@ 91 java.lang.System::arraycopy (0 bytes) (intrinsic)@ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large

* needs -XX:+UnlockDiagnosticVMOptions

Page 30: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

30

Advanced JIT & Runtime Optimizations - Escape Analysis

Page 31: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• Entire IR graph

• escaping allocations?

• not stored to a static field or non-static field of an external object,

• not returned from method,

• not passed as parameter to another method where it escapes.

31

Escape Analysis

Page 32: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram32

Escape Analysisallocated object doesn’t

escape the compiled method

allocated object not passed as a parameter

+

remove allocation and keep field values in registers

=

Page 33: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram33

Escape Analysis

allocated object is passed as a parameter

+

remove locks associated with object and use optimized compare instructions

=

allocated object doesn’t escape the compiled method

Page 34: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

34

Advanced JIT & Runtime Optimizations - Compressed

Oops

Page 35: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

A Java Object

35

Header Body

KlassMark Word

Array Length

Page 36: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Objects, Fields & Alignment• Objects are 8 byte aligned (default).

• Fields:

• are aligned by their type.

• can fill a gap that maybe required for alignment.

• are accessed using offset from the start of the object

36

Page 37: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram37

Mark Word - 32 bit vs 64 bit

Klass - 32 bit vs 64 bit

Array Length - 32 bit on both

boolean, byte, char, float, int, short - 32 bit on both

double, long - 64 bit on both

ILP32 vs. LP64 Field Sizes

Page 38: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Compressed OOPs

38

<wide-oop> = <narrow-oop-base> + (<narrow-oop> << 3) + <field-offset>

Page 39: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Compressed OOPs

39

Heap Size?<4 GB

(no encoding/decoding needed)

>4GB; <28GB (zero-based)

<wide-oop> = <narrow-oop> <narrow-oop> << 3

Page 40: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Compressed OOPs

40

Heap Size? >28 GB; <32 GB (regular)

>32 GB; <64 GB * (change alignment)

<wide-oop> =

<narrow-oop-base> + (<narrow-oop>

<< 3) + <field-offset>

<narrow-oop-base> + (<narrow-oop>

<< 4) + <field-offset>

Page 41: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Compressed OOPs

41

Heap Size? <4 GB >4GB; <28GB <32GB <64GB

Object Alignment? 8 bytes 8 bytes 8 bytes 16 bytes

Offset Required? No No Yes Yes

Shift by? No shift 3 3 4

Page 42: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Compressed Class Pointers

42

• JDK 8 —> Perm Gen Removal —> Class Data outside of heap

• Compressed class pointer space

• contains class metadata

• is a part of Metaspace

Page 43: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Compressed Class Pointers

43

PermGen Removal Overview by Coleen Phillimore + Jon Masamitsu @JavaOne 2013

Page 44: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

44

The Helpers - Garbage Collection.

Page 45: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Garbage Collection

45

Allocation

+Reclamation

Page 46: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

46

Garbage Collection - Allocation.

Page 47: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Generational Heap

47

Young Generation

Old Generation

Eden Survivors

Page 48: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

48

Fast Path Allocation - Thread Local Allocation Buffers

(TLABs).

Page 49: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Garbage Collection - Allocation

49

Most Allocations Eden SpaceFast Path

TLABs per thread:

• allocate lock-free

• bump-(your-own)-pointer allocation

• only co-ordinate when needing a new TLAB

Page 50: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

TLAB

50

EdenThread 1

Thread 2

Thread 3

Thread 4

TLAB TLAB TLAB TLAB TLAB

Thread 0

Page 51: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

51

(Min)TLABSize ResizeTLAB

TLABWasteTargetPercent PrintTLAB

Page 52: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

TLAB

52

Eden

TLAB TLAB TLAB TLAB TLAB

Page 53: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

TLAB

53

Eden

TLAB TLAB TLAB TLAB TLAB

TLABWasteTargetPercent (default = 1%)

Page 54: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

TLAB

54

Eden

TLAB TLAB TLAB TLAB TLAB

Page 55: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

TLAB

55

Eden

TLAB TLAB TLAB TLAB

(Min)TLABSize

Page 56: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

TLAB

56

Eden

TLAB TLAB TLAB

ResizeTLAB (default = TRUE)

Page 57: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

57

Allocation - Non Uniform Memory Access (NUMA) Aware

Allocator.

Page 58: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

NUMA

Processing Node 0

Memory Controller

DRAM Bank

Processing Node 2

Memory Controller

DRAM Bank

Processing Node 1

Memory Controller

DRAM Bank

Processing Node 3

Memory Controller

DRAM Bank

58

Page 59: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

UseNUMA

59

Processing Node 0

Memory Controller

DRAM Bank

Processing Node 1

Memory Controller

DRAM Bank

Thread 0

Area for

Node 0

Area for

Node 1

Thread 1

Thread 2

Eden

Page 60: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

60

UseNUMA UseNUMAInterleaving

UseAdaptiveNUMAChunkSizing NUMAStats

Page 61: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

61

Garbage Collection - Reclamation.

Page 62: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram62

Old Generation

Root Set

Young Generation

Garbage Collection -Reclamation via (Serial) Mark-Sweep-Compact

Page 63: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Garbage Collection -Reclamation via Parallel Mark-Compact

63

Old Generation

Page 64: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram64

Garbage Collection -Reclamation via Parallel Mark-Compact

Old Generation

Page 65: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram65

Garbage Collection -Reclamation via Parallel Mark-Compact

Old Generation

source region

destination region

Page 66: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram66

Garbage Collection -Reclamation via Parallel Mark-Compact

Old Generation

source region

destination region

Page 67: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram67

Garbage Collection -Reclamation via Parallel Mark-Compact

Old Generation

source region

destination region

source region

Page 68: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram68

Garbage Collection -Reclamation via Parallel Mark-Compact

Old Generation

source region

destination region

source region

Page 69: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram69

Garbage Collection -Reclamation via Parallel Mark-Compact

Old Generation

Page 70: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Garbage Collection -Reclamation via Mark-Sweep

70

Old Generation

Page 71: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Garbage Collection -Reclamation via Mark-Sweep

71

Old Generation

Page 72: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Garbage Collection - Reclamation via Scavenging

72

SurvivorEden

Young Generation

Page 73: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram73

SurvivorEden

Young Generation

Garbage Collection - Reclamation via Scavenging

Page 74: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram74

SurvivorEden

Young Generation

Garbage Collection - Reclamation via Scavenging

Page 75: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram75

Garbage Collection In OpenJDK HotSpot

The Throughput Collector -

• Young Collections - Parallel Scavenge

• Old Collections - Parallel Mark-Compact

Page 76: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram76

Garbage Collection In OpenJDK HotSpot

CMS Collector -

• Young Collections - Parallel New (similar to Parallel Scavenge)

• Old Collections - (Mostly Concurrent) Mark-Sweep

• Fallback Collections - Serial Mark-Sweep-Compact

Page 77: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram77

Garbage Collection In OpenJDK HotSpot

G1 Collector -

• Young and Mixed Collections - Compaction via Copying (similar to Parallel Scavenge)

• Fallback Collections - Serial Mark-Sweep-Compact

Page 78: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

78

What’s The #1 Contributor To A GC Pause Duration?

Page 79: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

Copying Costs!

79

Page 80: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• Size generations keeping your application’s object longevity and size in mind.

• short-lived; medium-lived; long-lived transient + permanent set.

• premature promotions are a big problem!

80

Garbage Collection - Tuning Recommendations

Page 81: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Generation Sizing

[Eden: 4972.0M(4972.0M)->0.0B(4916.0M) Survivors: 148.0M->204.0M Heap: 5295.8M(10.0G)->379.4M(10.0G)]

[Eden: Occupancy before GC(Eden size before GC)->Occupancy after GC(Eden size after GC) Survivors: Size before GC->Size after GC Heap: Occupancy before GC(Heap size before GC)->Occupancy after GC(Heap size after GC)]

81

Page 82: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Heap Information Plot

82

Occ

upan

cy in

MBs

0

7500

15000

22500

30000

TimeStamps

2500 3750 5000 6250 7500 8750 10000 11250 12500

Eden Occupancy Before GC Eden Size After GCSurvivor Size After GC Old Generation Occupancy After GCHeap Occupancy Before GC Heap Size

Page 83: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

83

What Are The ContributorS To GC Pause Frequency?

Page 84: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

Allocation Rate and Promotion Rate

84

Page 85: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

• The faster the generation gets “filled”; the sooner a GC is triggered.

• Premature promotions are a big problem!

• Size your generations and age your objects appropriately.

85

Garbage Collection - Tuning Recommendations

Page 86: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Plot Allocation & Promotion Rates

86

Page 87: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram87

Young Occupancy before GC Young Gen Size Old Gen Occupancy after GC

Heap Occupancy before GC Heap Occupancy after GC Heap Size

Timestamps

Page 88: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Allocation Rate

88

38.692: [GC (Allocation Failure) [PSYoungGen: 917486K->131054K(858112K)] 1004844K->224424K(2955264K), 0.0827570 secs] [Times: user=0.61 sys=0.00, real=0.08 secs]

51.013: [GC (Allocation Failure) [PSYoungGen: 858094K->50272K(777728K)] 951464K->239014K(2874880K), 0.1414536 secs] [Times: user=0.70 sys=0.07, real=0.14 secs]

Page 89: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Allocation Rate

89

38.692: [GC (Allocation Failure) [PSYoungGen: 917486K->131054K(858112K)] 1004844K->224424K(2955264K), 0.0827570 secs] [Times: user=0.61 sys=0.00, real=0.08 secs]

51.013: [GC (Allocation Failure) [PSYoungGen: 858094K->50272K(777728K)] 951464K->239014K(2874880K), 0.1414536 secs] [Times: user=0.70 sys=0.07, real=0.14 secs]

Allocation rate = (858094K-131054K)/()

Page 90: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Allocation Rate

90

38.692: [GC (Allocation Failure) [PSYoungGen: 917486K->131054K(858112K)] 1004844K->224424K(2955264K), 0.0827570 secs] [Times: user=0.61 sys=0.00, real=0.08 secs]

51.013: [GC (Allocation Failure) [PSYoungGen: 858094K->50272K(777728K)] 951464K->239014K(2874880K), 0.1414536 secs] [Times: user=0.70 sys=0.07, real=0.14 secs]

Allocation rate = (858094K-131054K)/(51.013-38.692) = 57.6MB/s

Page 91: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Promotion Rate

91

38.692: [GC (Allocation Failure) [PSYoungGen: 917486K->131054K(858112K)] 1004844K->224424K(2955264K), 0.0827570 secs] [Times: user=0.61 sys=0.00, real=0.08 secs]

51.013: [GC (Allocation Failure) [PSYoungGen: 858094K->50272K(777728K)] 951464K->239014K(2874880K), 0.1414536 secs] [Times: user=0.70 sys=0.07, real=0.14 secs]

Promotion rate = ((239014K - 50272K) – ()

Page 92: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Promotion Rate

92

38.692: [GC (Allocation Failure) [PSYoungGen: 917486K->131054K(858112K)] 1004844K->224424K(2955264K), 0.0827570 secs] [Times: user=0.61 sys=0.00, real=0.08 secs]

51.013: [GC (Allocation Failure) [PSYoungGen: 858094K->50272K(777728K)] 951464K->239014K(2874880K), 0.1414536 secs] [Times: user=0.70 sys=0.07, real=0.14 secs]

Promotion rate = ((239014K - 50272K) – (951464K - 858094K))/ ()

Page 93: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Promotion Rate

93

38.692: [GC (Allocation Failure) [PSYoungGen: 917486K->131054K(858112K)] 1004844K->224424K(2955264K), 0.0827570 secs] [Times: user=0.61 sys=0.00, real=0.08 secs]

51.013: [GC (Allocation Failure) [PSYoungGen: 858094K->50272K(777728K)] 951464K->239014K(2874880K), 0.1414536 secs] [Times: user=0.70 sys=0.07, real=0.14 secs]

Promotion rate = ((239014K - 50272K) – (951464K - 858094K))/ (51.013 - 38.692) = 7.56MB/s

Page 94: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

94

String Interning and Deduplication

Page 95: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram95

Java String Object

String1

String1 Object

char []

Page 96: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

96

What If String1 Is Interned?

Page 97: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram97

Java Interned String Object

String1

String1 Object

char []

StringX Object

char []

StringY Object

char []

Pool of Strings

Java Heap

Page 98: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram98

Java String Intern• unique + constant pool of strings

• hashtable

• default = 60013 (on LP64)

• if pool already contain string.equals(String1)?

• return string from the string pool

• else, add String1 to the pool; return its reference

Page 99: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram99

Java Interned String Objects

String1

String1 Object

char []

String2

String2 Object

char []

Page 100: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

100

If String1.intern() == String2.Intern() Then

String1.equals(String2) Is True.

Page 101: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram101

Java Interned String Objects

String1

String1 Object

char []

StringX Object

char []

StringY Object

char []

String2

Pool of Strings

Java Heap

Page 102: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

102

What If String1.equals(String2) And Both Are Not Interned*?

*And You Are Using The G1 Collector

Page 103: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram103

Java String Deduplication (G1)

String1

String1 Object

char []

String2

String2 Object

Page 104: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Further Reading

104

• Mark Price’s talk: https://qconlondon.com/presentation/hot-code-faster-code-addressing-jvm-warm

• https://wiki.openjdk.java.net/display/HotSpot/Server+Compiler+Inlining+Messages

• https://wiki.openjdk.java.net/display/HotSpot/EscapeAnalysis

• Compressed Class Pointers: https://youtu.be/AHtfza2Tkt0?t=754

• String Deduplication: http://openjdk.java.net/jeps/192

• Perm Gen Removal: http://www.infoq.com/articles/Java-PERMGEN-Removed

Page 105: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Appendix

105

Page 106: A Song of JIT and GC - QCon London 2020€¦ · ©2016 CodeKaram About Me • Java/JVM/GC Performance Engineer/Consultant • Worked at AMD, Sun, Oracle… • Worked with HotSpot

©2016 CodeKaram

Real-World Issues & Workarounds - JDK 8 update 45+

106

https://github.com/facebook/presto/commit/91e1b3bb6bbfffc62401025a24231cd388992d7c

https://gist.github.com/nileema/6fb667a215e95919242f