0% found this document useful (0 votes)
26 views

JVM Deep Dive

Uploaded by

Omesh Dhanushka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

JVM Deep Dive

Uploaded by

Omesh Dhanushka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

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

JIT Compiler Garbage Collector

HotSpot VM Runtime

5 Based on “Java Performance” p. 56


What happens between…

public class HelloWorld {


public static void main(String[] args) {
System.out.println(“Hello World”);
}
}

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

Byte Code Instructions

10
Demo Time!

javap -verbose -c HelloWorld.class

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

mov eax,DWORD PTR [rsp] ; get parameters


add rsp,0x8
mov edx,DWORD PTR [rsp]
add rsp,0x8
add eax,edx ; add parameters
movzx ebx,BYTE PTR [r13+0x1] ; dispatch next byte code
inc r13
movabs r10,0x109c72270
jmp QWORD PTR [r10+rbx*8]

15
Take Aways

• javac produces .class files which reflect the Java code


• .class files contain platform independent byte codes
• Inspect .class files with javap
• The interpreter is a complex beast

16
JIT Compilation

17
JIT?

• JIT = Just In Time


• "Profile-guided" optimisation
• Only hot code paths ("hot spots")

18
Compile Triggers
Counters in the Interpreter
• Method invocation counter
• Backedge counter (loop invocations)

19
JIT Compilation Strategies

• Client Compiler (C1): Less overhead, less optimisations


• Server Compiler (C2): Takes time, more aggressive optimisations
• Tiered Compilation: First C1, then C2

20
JIT Compiler and Interpreter

Interpreter C1 / C2
C1 / C2
trigger compilation

deoptimize optimize

Machine Code

21
Runtime Profiling

• Invariants: Loaded classes


• Statistics: Branches taken
• …

22
Optimisations
HotSpot implements dozens
• Dead Code Elimination
• Method Inlining
• Class Hierarchy Analysis
• …

23
Example Optimisation: Intrinsics
Hand-optimised "shortcuts" for certain JDK methods

public final class Math {


public static double abs(double a) {
return (a <= 0.0D) ? 0.0D - a : a;
}
}

24
Example Optimisation: Intrinsics
on x86, Math.abs() turns into:

andpd $dst, [0x7fffffffffffffff]

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

Add -XX:+PrintSafepointStatistics to show


the time-to-safepoint (TTSP) in GC logs

28
Inspecting Compilation

• Start with -XX:+PrintCompilation


• Use JITWatch (requires more flags, see docs)

29
Take Aways

• JIT compilation makes Java code fast


• JIT compilation relies on runtime information
• Cooperation needed between runtime, interpreter and JIT compiler

30
Memory
Management

31
Memory Regions

Stack Metaspace
Stack
Stack Heap

Code Cache

32
Garbage
Collectors

33
Memory Management on the JVM

1.Object x = new Object();


2. There is no step 2

34
Garbage Collector Tradeoffs

• Low latency: for human-facing systems


• High throughput: for batch processing systems
• Little memory overhead

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

Result: Increased GC performance

37
Heap Layout
Valid for most GC algorithms
• Young Generation: newly instantiated objects
• Old Generation (Tenured): objects that survived multiple garbage collections

Heap

Young Generation Old Generation

38
Common GC
Algorithms

39
SerialGC
• Activate with -XX:UseSerialGC
• Client applications with small heaps (<< 1 GB)

40 Image based on "Java Performance", page 86


ParallelGC
• Activate with -XX:UseParallelGC and -XX:UseParallelOldGC
• High throughput, higher pause times

41 Image based on "Java Performance", page 86


Concurrent Mark-Sweep (CMS)
• Activate with -XX:+UseConcMarkSweepGC (affects only old generation)
• Less throughput, smaller pause times

42 Image based on "Java Performance", page 88


Garbage First (G1)
• Activate with -XX:+UseG1GC
• Vastly different heap layout. Intended for large heaps (>> 8 GB)
• Less throughput, smaller pause times

43
Other GC Algorithms
For very large heaps (> 100 GB)
• Shenandoah (OpenJDK): Currently in alpha
• C4 (Azul Zing)

44
Which GC am I using?

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal


-version | grep -E “Use.*GC.*true"

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)

-Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps

47
Demo
Inspecting the GC
• Based on MinorGC demo by Gil Tene
• We analyse with Java Flight Recorder
• Popular alternative: GCViewer

48
Take Aways

• GC helps with memory management


• Different algorithms - Know their characteristics

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

• -Hydra- by arvalis (License: cc by-nc-nd 3.0)


• Jet Dragsters by J. Michael Raby (License: cc by-nc-nd 2.0)
• Stop! Go! by Nana B Agyei (License: cc by 2.0)
• 1GB DDR3 Memory Module by William Warby (License: cc by 2.0)
• _DSC8852 by Rusty Stewart (License: cc by nc nd 2.0)
• Night mechanic by Ali Bindawood (License: by-nd)

53

You might also like