JVM Deep Dive
JVM Deep Dive
Daniel Mitterdorfer
@dmitterd
‹#›
Behold! It will get scary
2
Topics
1 JVM Overview
2 Interpreter
3 JIT Compiler
4 Memory Management
3
What is a JVM?
Java® Virtual Machine Specification and multiple implementations
• HotSpot: reference implementation. Part of OpenJDK and Oracle JDK
• Azul Zing: Commercial performance-optimised JVM
• J9: implementation by IBM
• JRockit: implementation by Bea, now integrated into HotSpot
4
The HotSpot JVM
HotSpot VM Runtime
6
… and
Hello World!
7
“Compile”
javac HelloWorld.java
8
HelloWorld.class Hexdumped
0000000 ca fe ba be 00 00 00 34 00 1d 0a 00 06 00 0f 09
0000010 00 10 00 11 08 00 12 0a 00 13 00 14 07 00 15 07
0000020 00 16 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29
0000030 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e
0000040 75 6d 62 65 72 54 61 62 6c 65 01 00 04 6d 61 69
0000050 6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67
0000060 2f 53 74 72 69 6e 67 3b 29 56 01 00 0a 53 6f 75
0000070 72 63 65 46 69 6c 65 01 00 0f 48 65 6c 6c 6f 57
0000080 6f 72 6c 64 2e 6a 61 76 61 0c 00 07 00 08 07 00
0000090 17 0c 00 18 00 19 01 00 0c 48 65 6c 6c 6f 20 57
00000a0 6f 72 6c 64 21 07 00 1a 0c 00 1b 00 1c 01 00 0a
00000b0 48 65 6c 6c 6f 57 6f 72 6c 64 01 00 10 6a 61 76
00000c0 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74 01 00 10
00000d0 6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d
00000e0 01 00 03 6f 75 74 01 00 15 4c 6a 61 76 61 2f 69
9
Structure of a class file
Meta Information (Magic Byte, Version, …)
Constant Pool
10
Demo Time!
11
Topics
1 JVM Overview
2 Interpreter
3 JIT Compiler
4 Memory Management
12
The JVM: A stack-based machine
int sum = op0 + op1 turns into…
20: iload_1
21: iload_2
22: iadd
23: istore_3
13
Bytecode Execution
• C++ Interpreter
• Straightforward bytecode dispatching loop (switch)
• Only available in JVM debug builds
• Template Interpreter
• Generate assembler code per bytecode at startup
• Better optimised for current hardware
14
Generated assembler code for iadd
15
Take Aways
16
JIT Compilation
17
JIT?
18
Compile Triggers
Counters in the Interpreter
• Method invocation counter
• Backedge counter (loop invocations)
19
JIT Compilation Strategies
20
JIT Compiler and Interpreter
Interpreter C1 / C2
C1 / C2
trigger compilation
deoptimize optimize
Machine Code
21
Runtime Profiling
22
Optimisations
HotSpot implements dozens
• Dead Code Elimination
• Method Inlining
• Class Hierarchy Analysis
• …
23
Example Optimisation: Intrinsics
Hand-optimised "shortcuts" for certain JDK methods
24
Example Optimisation: Intrinsics
on x86, Math.abs() turns into:
25
Safepoints
26
Safepoints
How to remove compiled machine code in a busy application?
1. Halt every application thread ("safepoint")
2. Replace machine code with interpreted code
27
Safepoints
Used for different tasks in the JVM
• Garbage Collection
• Thread Dumps (e.g. used by sampling profilers)
• Deadlock Detection
28
Inspecting Compilation
29
Take Aways
30
Memory
Management
31
Memory Regions
Stack Metaspace
Stack
Stack Heap
Code Cache
32
Garbage
Collectors
33
Memory Management on the JVM
34
Garbage Collector Tradeoffs
35
Weak Generational Hypothesis
Most objects survive for only a short period of time
36 Source: http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/generations.html#sthref18
Weak Generational Hypothesis
Most GC algorithms are based on this assumption
• Split the heap into "generations"
• Collect generations separately
37
Heap Layout
Valid for most GC algorithms
• Young Generation: newly instantiated objects
• Old Generation (Tenured): objects that survived multiple garbage collections
Heap
38
Common GC
Algorithms
39
SerialGC
• Activate with -XX:UseSerialGC
• Client applications with small heaps (<< 1 GB)
43
Other GC Algorithms
For very large heaps (> 100 GB)
• Shenandoah (OpenJDK): Currently in alpha
• C4 (Azul Zing)
44
Which GC am I using?
45
GC Tuning
Measure, measure, measure
• Know your application's behaviour and SLAs
• Turn the least amount of knobs (around 200 GC related JVM flags)
46
GC Tuning
Starting Point (flags will change with JEP 271 in JDK 9)
47
Demo
Inspecting the GC
• Based on MinorGC demo by Gil Tene
• We analyse with Java Flight Recorder
• Popular alternative: GCViewer
48
Take Aways
49
Getting started yourself
Download the OpenJDK source code at http://openjdk.java.net and dive in!
50
Slides
• http://bit.ly/jvm-deep-dive-javaland
51
Q&A
?
52
Image Credit
53