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.
1
THE	
PERFORMANCE	
ENGINEER’S	
GUIDE	TO	
HOTSPOT	VIRTUAL	
MACHINE'S	
EXECUTION	ENGINE
2
About Me …
Java/JVM/GC performance engineer
Current Life: Consultant who solves Managed Runtime
performance issues
Past ...
©2017 CodeKaram
Establishing the Characters
and The Plot
3
• HotSpot Virtual Machine
• The Helpers
• The Execution Engine
...
©2017 CodeKaram
The Storyline
4
• Dive into the Influence of Footprint, Throughput
and Responsiveness on the HotSpot Execut...
5
HotSpot Virtual Machine
©2017 CodeKaram
Let’s Peek Under The Hood
6
Java Application
Java
API
Java VM
OS + Hardware
©2017 CodeKaram7
Java Application
Java
API
Java VM
OS + HardwareJRE
Java Runtime Environment
©2017 CodeKaram
The Helpers
8
Java VM
Execution Engine
Runtime
©2017 CodeKaram
The Helpers
9
Java VM
Execution Engine
Runtime
10
The Execution Engine
©2017 CodeKaram
Execution Engine
11
Heap
Management
- Garbage
Collection
Compilation
©2017 CodeKaram12
Allocation
+
Reclamation
Heap Management
©2017 CodeKaram
Compilation
13
Adaptive
Optimization
Ahead
Of
Time
Profile-
guided
Static
©2017 CodeKaram
The Helpers
14
Java VM
Execution Engine
Runtime
15
The Runtime
©2017 CodeKaram
HotSpot VM
16
Runtime
VM Class
loading
Interpreter
Bytecode
Verification,
Exception
Handling,
Synchronizat...
©2017 CodeKaram
Runtime Goal
17
Bytecode
Native code
18
What Drives Innovations in
Performance?
©2017 CodeKaram
The Trifecta of Performance
• Responsiveness
• Footprint
• Throughput
19
©2017 CodeKaram
If I send a stimulus now, how much time
’til I get a response back?
20
Responsiveness
©2017 CodeKaram
Can I fit in more?
OR
How can I optimize out redundancy and
compact?
21
FootPrint
©2017 CodeKaram
How can I maximize the operations per
second of my system?
22
Throughput
23
Responsiveness, the Execution
Engine and the Runtime
©2017 CodeKaram
Responsiveness and GC
24
GC
Faster Allocation Paths
Faster Reclamation
More Work Done Concurrently
©2017 CodeKaram
Responsiveness and
Compilation
25
Compilation
Ahead-Of-Time
Adaptive Optimizer
Locking Improvements
©2017 CodeKaram
Responsiveness and the
Runtime
26
Runtime
Locking Improvements
Class Data Sharing
©2017 CodeKaram
Uncontended Locks
27
A	single	thread	‘t’	is	executing	a	synchronized	
method
©2017 CodeKaram
Uncontended Locks
• Deflated locks
• aka light weight locks
• Compare and Swap (CAS) stores pointer to a
lo...
©2017 CodeKaram
Contended Locks
29
A	different	thread	‘u’	wants	to	enter	the	
synchronized	method	that	is	already	locked	by...
©2017 CodeKaram
Contended Locks
• Inflated locks
• ak heavy weight locks
• Slower path
• Object monitors maintain their wai...
31
Wait Queue
Object Monitor
Entry Queue
Monitor.
Enter()
Monitor.
Exit()
©2017 CodeKaram
Locking Improvements
• Biased Locking
• Lock Elision
• Lock Coarsening
• Contended Locking - Quick Path
32
©2017 CodeKaram
Contended Lock
33
Runtime	–
contention	/	inflated	lock
?
ObjectMonitor::enter
ObjectSynchronizer::slow_ent...
©2017 CodeKaram
Contended Lock
Optimization
34
ObjectSynchronizer::quick_enter
Runtime	–
contention	/	inflated	lock
?
Obje...
35
Footprint, the Execution
Engine and the Runtime
©2017 CodeKaram
Footprint and GC
36
GC
Compaction
Algorithm Refinements In Reducing Allocations
Compressed Headers
©2017 CodeKaram
FootPrint and Compilation
37
Compilation
Inlining
Escape Analysis
Zombie Code (Deopt)
38
Advanced Optimization -
Escape Analysis
©2017 CodeKaram
• Entire IR graph
• escaping allocations?
• not stored to a static field or non-static field of
an external ...
©2017 CodeKaram40
Escape Analysis
allocated object doesn’t
escape the compiled method
allocated object not
passed as a par...
©2017 CodeKaram41
Escape Analysis
allocated object is
passed as a parameter
+
perform lock elision and use optimized
compa...
©2017 CodeKaram
FootPrint and the Runtime
42
Runtime
Compressed Oops And Class Pointers
Code Caches
Class Data Sharing
43
Advanced Optimizations -
Compressed Oops +
Compressed Class Pointers
44
A Java Object
©2017 CodeKaram45
Header Body
46
A Java Object Header
©2017 CodeKaram
A Java Object Header
47
Header
Klass
Mark
Word
Array
Length
©2017 CodeKaram
Compressed OOPs and
Compressed Class Pointers
48
# Running 64-bit HotSpot VM.
# Using compressed oop with ...
49
Let’s talk about the 3-bit shift
first!
50
All Java objects are 8 byte
aligned.
51
IF we left shift by 3, we can
have 2^35 = 32GB of
addressable space.
©2017 CodeKaram
Compressed OOPs
52
<wide-oop> = <narrow-oop-base> +
(<narrow-oop> << 3) + <field-offset>
©2017 CodeKaram
Compressed OOPs
53
Heap Size?
<4 GB
(no encoding/
decoding needed)
>4GB; <28GB
(zero-based)
<wide-oop> = <...
©2017 CodeKaram
Compressed OOPs
54
Heap Size?
>28 GB; <32 GB
(regular)
>32 GB; <64 GB *
(change alignment)
<wide-oop> =
<n...
©2017 CodeKaram
Compressed OOPs
55
Heap Size? <4 GB
>4GB;
<28GB
<32GB <64GB
Object
Alignment?
8 bytes 8 bytes 8 bytes 16 b...
©2017 CodeKaram
Compressed Class Pointers
56
• JDK 8 —> Perm Gen Removal —> Class Data outside of
heap
• Compressed class ...
©2017 CodeKaram
Compressed Class Pointers
57
PermGen Removal Overview by Coleen Phillimore + Jon Masamitsu @JavaOne 2013
58
Throughput, the Execution
Engine and the Runtime
©2017 CodeKaram
Throughput and GC
59
GC
Multi-Threaded Work Stealing
Multi-Generational Heap
Multi-Phase Marking
(Incremen...
©2017 CodeKaram
Throughput and Compilation
60
Compilation
JIT
Intrinsics
Vectorization
Loop Unrolling
61
JIT Optimization - Intrinsics
©2017 CodeKaram62
Without Intrinsics
Java Method
JIT Compilation
Execute Generated Code
©2017 CodeKaram63
Intrinsics
Java Method
JIT Compilation
Execute Optimized Code
Call Hand-
Optimized
Assembly Code
©2017 CodeKaram
Throughput and the Runtime
64
Runtime
Compressed Oops
Compressed Class Pointers
65
HotSpot’s GC Facts and
Performance Innovations
©2017 CodeKaram
GC
66
Responsiveness Throughput Footprint
Faster Allocation
Paths
Multi-Threaded
Work Stealing
Compaction
...
67
Footprint Optimizations
68
String Interning and
Deduplication
©2017 CodeKaram69
Java String Object
String1
String1
Object
char []
70
What If String1 Is Interned?
©2017 CodeKaram71
Java Interned String Object
String1
String1
Object
char []
StringX
Object
char []
StringY
Object
char []...
©2017 CodeKaram72
Java Interned String Objects
String1
String1
Object
char []
String2
String2
Object
char []
73
If String1.intern() ==
String2.intern() then
String1.equals(String2) is TRUE.
©2017 CodeKaram74
Java Interned String Objects
String1
String1
Object
char []
StringX
Object
char []
StringY
Object
char [...
75
What If String1.equals(String2)
And Both Are Not Interned*?
*
And You Are Using The G1 Collector
©2017 CodeKaram76
Java String Deduplication
(G1)
String1
String1
Object
char []
String2
String2
Object
©2017 CodeKaram
Almost all GCs in OpenJDK HotSpot are
generational.
77
GC Fact #1
©2017 CodeKaram
GC
78
Responsiveness Throughput Footprint
Faster
Allocation Paths
Multi-Threaded
Work Stealing
Compaction
...
©2017 CodeKaram79
Young
Generation
Old
Generation
Eden Survivors
©2017 CodeKaram
The Generational Java Heap
Eden S0 S1 Old Generation
Young Generation
Allocations Survivors
Tenured
80
©2017 CodeKaram
GC
81
Faster
Allocation
Paths
Multi-Threaded
Work Stealing
Faster
Reclamation
Multi-
Generational
Heap
Mor...
82
Garbage Collection -
Allocation.
©2017 CodeKaram
GC
83
Faster
Allocation
Paths
Faster
Reclamation
Multi-
Generational
Heap
Allocation
©2017 CodeKaram84
Eden
Allocations
Survivors
Young Generation
85
Fast Path Allocation ==
Lock-Free Allocation ==
Threads Allocate Into Their
Local Allocation Buffer (LAB)s
©2017 CodeKaram86
Eden
TLAB TLAB TLAB TLAB TLAB
TLAB = Thread Local Allocation Buffer
©2017 CodeKaram87
EdenThread 1
Thread 2
Thread 3
Thread 4
TLAB TLAB TLAB TLAB TLAB
Thread 0
©2017 CodeKaram88
Eden
Allocations
Survivors
Young Generation
S0 S1
©2017 CodeKaram89
Eden
Allocations
Survivors
Young Generation
S0 S1
90
Allocation - Non Uniform
Memory Access (NUMA) Aware
Allocator.
©2017 CodeKaram
NUMA
Processing
Node 0
Memory
Controller
DRAM
Bank
Processing
Node 2
Memory
Controller
DRAM
Bank
Processin...
©2017 CodeKaram
UseNUMA
92
Processing
Node 0
Memory
Controller
DRAM
Bank
Processing
Node 1
Memory
Controller
DRAM
Bank
Thr...
93
UseNUMA
UseNUMAInterleaving
UseAdaptiveNUMAChunkSizing
NUMAStats
94
Garbage Collection -
Reclamation.
©2017 CodeKaram
GC
95
Multi-Threaded
Work Stealing
Faster
Reclamation
Multi-
Generational
Heap
More Work
Done
Concurrently...
©2017 CodeKaram96
Young Generation Old Generation
Eden S0 S1
©2017 CodeKaram97
*Similar GC
Algorithms for
generational
OpenJDK HotSpot
Different GC Algorithms
for OpenJDK Hotspot
Youn...
©2017 CodeKaram98
*Similar GC
Algorithms for
generational
OpenJDK HotSpot
Different GC Algorithms
for OpenJDK Hotspot
Youn...
©2017 CodeKaram99
Throughput
Maximizer • Multi-
generation heap
• Multi-threaded
workers
• Compacting
collector
• Faster
r...
©2017 CodeKaram100
Latency
Sensitive
CMS GC
• Multi-
generation heap
• Multi-threaded
workers*
• More work
done
concurrent...
©2017 CodeKaram101 ©2017 CodeKaram
• Multi-
generation heap
• Multi-threaded
workers
• More work
done
concurrently
• Multi...
©2017 CodeKaram102
Throughput
Maximizer
Latency
Sensitive
Parallel GC
CMS GC
G1 GC
©2017 CodeKaram
HotSpot GCs
103
HotSpot’s Compilation Engine
Performance Innovations
©2017 CodeKaram
Compilation Engine
104
Responsiveness Throughput Footprint
Ahead-Of-Time JIT Inlining
Adaptive
Optimizer
I...
©2017 CodeKaram
Compilation
105
Adaptive
Optimization
Ahead
Of
Time
Profile-
guided
Static
©2017 CodeKaram
• CompileThreshold
• Identify root of compilation
• Method Compilation or On-stack
replacement (Loop)?
106...
107
JIT Optimization - Client
Compiler (C1)
©2017 CodeKaram
• Start in interpreter
• CompileThreshold = 1500
• Compile with client compiler
• Few optimizations
108
Cl...
109
JIT Optimization - Server
Compiler (C2)
©2017 CodeKaram
• Start in interpreter
• CompileThreshold = 10000
• Compile with optimized server compiler
• High performa...
111
JIT Optimization - Tiered
Compilation
112
©2017 CodeKaram
• Start in interpreter
• Tiered optimization with client compiler
• Code profiled information
• Enable serv...
©2017 CodeKaram114
Tiered Compilation - Effect
on Code Cache
Tiered
Compilation
Code
Cache
Compiled
Native Code
©2017 CodeKaram
• C1 compilation threshold for tiered is about a 100
invocations
• Tiered Compilation has a lot more profil...
116
Advanced Optimizations -
Adaptive Optimization
©2017 CodeKaram117
The Plot
Startup
(Adaptive) JIT
Optimizations
Profiling of
critical hot-
spots
Interpreter
©2017 CodeKaram118
The Plot
Startup
(Adaptive) JIT
Optimizations
Profiling of
critical hot-
spots
Interpreter
119
Adaptive Optimization:
Dynamic Deoptimization
120
Dynamic Deoptimization
©2017 CodeKaram
• Oopsie!
• dependencies invalidation
• classes unloading and redefinition
• uncommon path in compiled code...
122
Advanced Optimization -
Vectorization
123
Vectorization in HotSpot
VM??
©2017 CodeKaram
Vectorization
124
• Utilize SIMD (Single Instruction Multiple Data)
instructions offered by the processor....
125
Advanced Optimization -
Auto-Vectorization
126
Auto-Vectorization in
HotSpot VM??
127
Look Ma, no stubs!!
128
©2017 CodeKaram
SuperWord Level Parallelism
129
http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
©2017 CodeKaram
SuperWord Level Parallelism
130
• Loop Unrolling
• Alignment Analysis
• Pre-Optimization
• Identifying Adj...
131
That’s all I got! :)
©2017 CodeKaram
Further Reading
132
• https://wiki.openjdk.java.net/display/HotSpot/
Server+Compiler+Inlining+Messages
• h...
Appendix
133
134
HotSpot’s Runtime Performance
Innovations
©2017 CodeKaram
Runtime
135
Responsiveness Throughput Footprint
Locking
Improvements
Compressed
Oops
Compressed
Oops And C...
136
Advanced Optimizations -
Compressed Oops +
Compressed Class Pointers
©2017 CodeKaram
Objects, Fields & Alignment
• Objects are 8 byte aligned (default).
• Fields:
• are aligned by their type....
138
Java Object Layout in Memory
©2017 CodeKaram139
-Tool to analyze Java object layout :)
Java Object Layout (JOL)
©2017 CodeKaram
JOL Command Line Options
$ java -jar jol-cli.jar
Usage: jol-cli.jar <mode> [optional arguments]*
Available...
©2017 CodeKaram
JOL Command Line Options
$ java -jar jol-cli.jar
Usage: jol-cli.jar <mode> [optional arguments]*
Available...
©2017 CodeKaram
Compressed OOPs and
Compressed Class Pointers
142
# Running 64-bit HotSpot VM.
# Using compressed oop with...
©2017 CodeKaram
Compressed OOPs and
Compressed Class Pointers
143
# Running 64-bit HotSpot VM.
# Using compressed oop with...
144
A Java Object Header
©2017 CodeKaram
A Java Object Header
145
Header
Klass
Mark
Word
Array
Length
146
Java Object Layout Samples -
Uncompressed
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00
...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00
...
154
Java Object Layout Samples -
Compressed
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 ...
©2017 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 ...
162
Java Object Layout Header
Comparison
©2017 CodeKaram
Compressed vs
Uncompressed
Compressed:
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes
ex...
©2017 CodeKaram
Compressed vs
Uncompressed
Compressed:
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes ex...
©2017 CodeKaram
Compressed OOPs and
Compressed Class Pointers
165
# Running 64-bit HotSpot VM.
# Using compressed oop with...
166
Java Object Layout Samples -
UnCompressed
©2017 CodeKaram
JOL: Class
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 16 (object header) N/A
1...
©2017 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 16 (object header) N/A
16 8 Constru...
©2017 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 16 (object header) N/A
16 8 Constru...
170
Java Object Layout Samples -
Compressed
©2017 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 Constru...
©2017 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 Constru...
©2017 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 Constru...
©2017 CodeKaram
Compressed OOPs and
Compressed Class Pointers
174
# Running 64-bit HotSpot VM.
# Using compressed oop with...
Upcoming SlideShare
Loading in …5
×

The Performance Engineer's Guide to Java (HotSpot) Virtual Machine

1,375 views

Published on

Monica Beckwith has worked with the Java Virtual Machine for more than a decade not just optimizing the JVM heuristics, but also improving the Just-in-time (JIT) code quality for various processor architectures as well as working with the garbage collectors and improving garbage collection for server systems.

During this talk, Monica will cover a few JIT and Runtime optimizations and she will dive into the HotSpot garbage collection and provide an overview of the various garbage collectors available in HotSpot.

Published in: Technology
  • Be the first to comment

The Performance Engineer's Guide to Java (HotSpot) Virtual Machine

  1. 1. 1 THE PERFORMANCE ENGINEER’S GUIDE TO HOTSPOT VIRTUAL MACHINE'S EXECUTION ENGINE
  2. 2. 2 About Me … Java/JVM/GC performance engineer Current Life: Consultant who solves Managed Runtime performance issues Past Life: Worked with Oracle, Sun, AMD… Worked with HotSpot VM: JVM heuristics, JIT compiler, GCs: Parallel(Old) GC, G1 GC, CMS GC www.codekaram.com https://www.linkedin.com/in/monicabeckwith Tweet @mon_beck
  3. 3. ©2017 CodeKaram Establishing the Characters and The Plot 3 • HotSpot Virtual Machine • The Helpers • The Execution Engine • The Runtime • Driving Innovations in Performance • The Trifecta
  4. 4. ©2017 CodeKaram The Storyline 4 • Dive into the Influence of Footprint, Throughput and Responsiveness on the HotSpot Execution Engine and Runtime. • Various Optimizations.
  5. 5. 5 HotSpot Virtual Machine
  6. 6. ©2017 CodeKaram Let’s Peek Under The Hood 6 Java Application Java API Java VM OS + Hardware
  7. 7. ©2017 CodeKaram7 Java Application Java API Java VM OS + HardwareJRE Java Runtime Environment
  8. 8. ©2017 CodeKaram The Helpers 8 Java VM Execution Engine Runtime
  9. 9. ©2017 CodeKaram The Helpers 9 Java VM Execution Engine Runtime
  10. 10. 10 The Execution Engine
  11. 11. ©2017 CodeKaram Execution Engine 11 Heap Management - Garbage Collection Compilation
  12. 12. ©2017 CodeKaram12 Allocation + Reclamation Heap Management
  13. 13. ©2017 CodeKaram Compilation 13 Adaptive Optimization Ahead Of Time Profile- guided Static
  14. 14. ©2017 CodeKaram The Helpers 14 Java VM Execution Engine Runtime
  15. 15. 15 The Runtime
  16. 16. ©2017 CodeKaram HotSpot VM 16 Runtime VM Class loading Interpreter Bytecode Verification, Exception Handling, Synchronization , Thread Management, ….http://openjdk.java.net/groups/hotspot/ docs/RuntimeOverview.html
  17. 17. ©2017 CodeKaram Runtime Goal 17 Bytecode Native code
  18. 18. 18 What Drives Innovations in Performance?
  19. 19. ©2017 CodeKaram The Trifecta of Performance • Responsiveness • Footprint • Throughput 19
  20. 20. ©2017 CodeKaram If I send a stimulus now, how much time ’til I get a response back? 20 Responsiveness
  21. 21. ©2017 CodeKaram Can I fit in more? OR How can I optimize out redundancy and compact? 21 FootPrint
  22. 22. ©2017 CodeKaram How can I maximize the operations per second of my system? 22 Throughput
  23. 23. 23 Responsiveness, the Execution Engine and the Runtime
  24. 24. ©2017 CodeKaram Responsiveness and GC 24 GC Faster Allocation Paths Faster Reclamation More Work Done Concurrently
  25. 25. ©2017 CodeKaram Responsiveness and Compilation 25 Compilation Ahead-Of-Time Adaptive Optimizer Locking Improvements
  26. 26. ©2017 CodeKaram Responsiveness and the Runtime 26 Runtime Locking Improvements Class Data Sharing
  27. 27. ©2017 CodeKaram Uncontended Locks 27 A single thread ‘t’ is executing a synchronized method
  28. 28. ©2017 CodeKaram Uncontended Locks • Deflated locks • aka light weight locks • Compare and Swap (CAS) stores pointer to a lock record in the object header. 28
  29. 29. ©2017 CodeKaram Contended Locks 29 A different thread ‘u’ wants to enter the synchronized method that is already locked by thread ‘t’
  30. 30. ©2017 CodeKaram Contended Locks • Inflated locks • ak heavy weight locks • Slower path • Object monitors maintain their wait-sets 30
  31. 31. 31 Wait Queue Object Monitor Entry Queue Monitor. Enter() Monitor. Exit()
  32. 32. ©2017 CodeKaram Locking Improvements • Biased Locking • Lock Elision • Lock Coarsening • Contended Locking - Quick Path 32
  33. 33. ©2017 CodeKaram Contended Lock 33 Runtime – contention / inflated lock ? ObjectMonitor::enter ObjectSynchronizer::slow_enter
  34. 34. ©2017 CodeKaram Contended Lock Optimization 34 ObjectSynchronizer::quick_enter Runtime – contention / inflated lock ? ObjectMonitor::enter ObjectSynchronizer::slow_enter
  35. 35. 35 Footprint, the Execution Engine and the Runtime
  36. 36. ©2017 CodeKaram Footprint and GC 36 GC Compaction Algorithm Refinements In Reducing Allocations Compressed Headers
  37. 37. ©2017 CodeKaram FootPrint and Compilation 37 Compilation Inlining Escape Analysis Zombie Code (Deopt)
  38. 38. 38 Advanced Optimization - Escape Analysis
  39. 39. ©2017 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. 39 Escape Analysis
  40. 40. ©2017 CodeKaram40 Escape Analysis allocated object doesn’t escape the compiled method allocated object not passed as a parameter + remove allocation and lock (lock elision) and keep field values in registers =
  41. 41. ©2017 CodeKaram41 Escape Analysis allocated object is passed as a parameter + perform lock elision and use optimized compare instructions = allocated object doesn’t escape the compiled method
  42. 42. ©2017 CodeKaram FootPrint and the Runtime 42 Runtime Compressed Oops And Class Pointers Code Caches Class Data Sharing
  43. 43. 43 Advanced Optimizations - Compressed Oops + Compressed Class Pointers
  44. 44. 44 A Java Object
  45. 45. ©2017 CodeKaram45 Header Body
  46. 46. 46 A Java Object Header
  47. 47. ©2017 CodeKaram A Java Object Header 47 Header Klass Mark Word Array Length
  48. 48. ©2017 CodeKaram Compressed OOPs and Compressed Class Pointers 48 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  49. 49. 49 Let’s talk about the 3-bit shift first!
  50. 50. 50 All Java objects are 8 byte aligned.
  51. 51. 51 IF we left shift by 3, we can have 2^35 = 32GB of addressable space.
  52. 52. ©2017 CodeKaram Compressed OOPs 52 <wide-oop> = <narrow-oop-base> + (<narrow-oop> << 3) + <field-offset>
  53. 53. ©2017 CodeKaram Compressed OOPs 53 Heap Size? <4 GB (no encoding/ decoding needed) >4GB; <28GB (zero-based) <wide-oop> = <narrow-oop> <narrow-oop> << 3
  54. 54. ©2017 CodeKaram Compressed OOPs 54 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>
  55. 55. ©2017 CodeKaram Compressed OOPs 55 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
  56. 56. ©2017 CodeKaram Compressed Class Pointers 56 • JDK 8 —> Perm Gen Removal —> Class Data outside of heap • Compressed class pointer space • contains class metadata • is a part of Metaspace
  57. 57. ©2017 CodeKaram Compressed Class Pointers 57 PermGen Removal Overview by Coleen Phillimore + Jon Masamitsu @JavaOne 2013
  58. 58. 58 Throughput, the Execution Engine and the Runtime
  59. 59. ©2017 CodeKaram Throughput and GC 59 GC Multi-Threaded Work Stealing Multi-Generational Heap Multi-Phase Marking (Incremental) Compaction
  60. 60. ©2017 CodeKaram Throughput and Compilation 60 Compilation JIT Intrinsics Vectorization Loop Unrolling
  61. 61. 61 JIT Optimization - Intrinsics
  62. 62. ©2017 CodeKaram62 Without Intrinsics Java Method JIT Compilation Execute Generated Code
  63. 63. ©2017 CodeKaram63 Intrinsics Java Method JIT Compilation Execute Optimized Code Call Hand- Optimized Assembly Code
  64. 64. ©2017 CodeKaram Throughput and the Runtime 64 Runtime Compressed Oops Compressed Class Pointers
  65. 65. 65 HotSpot’s GC Facts and Performance Innovations
  66. 66. ©2017 CodeKaram GC 66 Responsiveness Throughput Footprint Faster Allocation Paths Multi-Threaded Work Stealing Compaction Faster Reclamation Multi- Generational Heap Algorithm Refinements In Reducing AllocationsMore Work Done Concurrently Multi-Phase Marking Incremental Compaction Compressed Headers
  67. 67. 67 Footprint Optimizations
  68. 68. 68 String Interning and Deduplication
  69. 69. ©2017 CodeKaram69 Java String Object String1 String1 Object char []
  70. 70. 70 What If String1 Is Interned?
  71. 71. ©2017 CodeKaram71 Java Interned String Object String1 String1 Object char [] StringX Object char [] StringY Object char [] Pool of Strings Java Heap
  72. 72. ©2017 CodeKaram72 Java Interned String Objects String1 String1 Object char [] String2 String2 Object char []
  73. 73. 73 If String1.intern() == String2.intern() then String1.equals(String2) is TRUE.
  74. 74. ©2017 CodeKaram74 Java Interned String Objects String1 String1 Object char [] StringX Object char [] StringY Object char [] String2 Pool of Strings Java Heap
  75. 75. 75 What If String1.equals(String2) And Both Are Not Interned*? * And You Are Using The G1 Collector
  76. 76. ©2017 CodeKaram76 Java String Deduplication (G1) String1 String1 Object char [] String2 String2 Object
  77. 77. ©2017 CodeKaram Almost all GCs in OpenJDK HotSpot are generational. 77 GC Fact #1
  78. 78. ©2017 CodeKaram GC 78 Responsiveness Throughput Footprint Faster Allocation Paths Multi-Threaded Work Stealing Compaction Faster Reclamation Multi- Generational Heap Algorithm Refinements In Reducing AllocationsMore Work Done Concurrently Multi-Phase Marking (Incremental) Compaction Compressed Headers
  79. 79. ©2017 CodeKaram79 Young Generation Old Generation Eden Survivors
  80. 80. ©2017 CodeKaram The Generational Java Heap Eden S0 S1 Old Generation Young Generation Allocations Survivors Tenured 80
  81. 81. ©2017 CodeKaram GC 81 Faster Allocation Paths Multi-Threaded Work Stealing Faster Reclamation Multi- Generational Heap More Work Done Concurrently Multi-Phase Marking (Incremental) Compaction Allocation + Reclamation & Promotions
  82. 82. 82 Garbage Collection - Allocation.
  83. 83. ©2017 CodeKaram GC 83 Faster Allocation Paths Faster Reclamation Multi- Generational Heap Allocation
  84. 84. ©2017 CodeKaram84 Eden Allocations Survivors Young Generation
  85. 85. 85 Fast Path Allocation == Lock-Free Allocation == Threads Allocate Into Their Local Allocation Buffer (LAB)s
  86. 86. ©2017 CodeKaram86 Eden TLAB TLAB TLAB TLAB TLAB TLAB = Thread Local Allocation Buffer
  87. 87. ©2017 CodeKaram87 EdenThread 1 Thread 2 Thread 3 Thread 4 TLAB TLAB TLAB TLAB TLAB Thread 0
  88. 88. ©2017 CodeKaram88 Eden Allocations Survivors Young Generation S0 S1
  89. 89. ©2017 CodeKaram89 Eden Allocations Survivors Young Generation S0 S1
  90. 90. 90 Allocation - Non Uniform Memory Access (NUMA) Aware Allocator.
  91. 91. ©2017 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 91
  92. 92. ©2017 CodeKaram UseNUMA 92 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
  93. 93. 93 UseNUMA UseNUMAInterleaving UseAdaptiveNUMAChunkSizing NUMAStats
  94. 94. 94 Garbage Collection - Reclamation.
  95. 95. ©2017 CodeKaram GC 95 Multi-Threaded Work Stealing Faster Reclamation Multi- Generational Heap More Work Done Concurrently Multi-Phase Marking (Incremental) Compaction Reclamation
  96. 96. ©2017 CodeKaram96 Young Generation Old Generation Eden S0 S1
  97. 97. ©2017 CodeKaram97 *Similar GC Algorithms for generational OpenJDK HotSpot Different GC Algorithms for OpenJDK Hotspot Young Generation Old Generation
  98. 98. ©2017 CodeKaram98 *Similar GC Algorithms for generational OpenJDK HotSpot Different GC Algorithms for OpenJDK Hotspot Young Generation Old Generation
  99. 99. ©2017 CodeKaram99 Throughput Maximizer • Multi- generation heap • Multi-threaded workers • Compacting collector • Faster reclamation via PLABS (Promotion Local Area Buffers) Parallel GC
  100. 100. ©2017 CodeKaram100 Latency Sensitive CMS GC • Multi- generation heap • Multi-threaded workers* • More work done concurrently • Multi-phased marking
  101. 101. ©2017 CodeKaram101 ©2017 CodeKaram • Multi- generation heap • Multi-threaded workers • More work done concurrently • Multi-phased marking • Multi- generation heap • Multi-threaded workers • Incrementally compacting collector • Faster reclamation via PLABS Throughput Maximizer Latency Sensitive G1 GC
  102. 102. ©2017 CodeKaram102 Throughput Maximizer Latency Sensitive Parallel GC CMS GC G1 GC ©2017 CodeKaram HotSpot GCs
  103. 103. 103 HotSpot’s Compilation Engine Performance Innovations
  104. 104. ©2017 CodeKaram Compilation Engine 104 Responsiveness Throughput Footprint Ahead-Of-Time JIT Inlining Adaptive Optimizer Intrinsics Escape Analysis Locking Improvements Vectorization Zombie Code (Deopt) Loop Unrolling
  105. 105. ©2017 CodeKaram Compilation 105 Adaptive Optimization Ahead Of Time Profile- guided Static
  106. 106. ©2017 CodeKaram • CompileThreshold • Identify root of compilation • Method Compilation or On-stack replacement (Loop)? 106 Identifying Performance Critical Methods
  107. 107. 107 JIT Optimization - Client Compiler (C1)
  108. 108. ©2017 CodeKaram • Start in interpreter • CompileThreshold = 1500 • Compile with client compiler • Few optimizations 108 Client Compiler
  109. 109. 109 JIT Optimization - Server Compiler (C2)
  110. 110. ©2017 CodeKaram • Start in interpreter • CompileThreshold = 10000 • Compile with optimized server compiler • High performance optimizations 110 Server Compiler
  111. 111. 111 JIT Optimization - Tiered Compilation
  112. 112. 112
  113. 113. ©2017 CodeKaram • Start in interpreter • Tiered optimization with client compiler • Code profiled information • Enable server compiler 113 Tiered Compilation
  114. 114. ©2017 CodeKaram114 Tiered Compilation - Effect on Code Cache Tiered Compilation Code Cache Compiled Native Code
  115. 115. ©2017 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 (240MB vs 48MB) • Need more? Use -XX:ReservedCodeCacheSize 115 Tiered Compilation - Effect on Code Cache
  116. 116. 116 Advanced Optimizations - Adaptive Optimization
  117. 117. ©2017 CodeKaram117 The Plot Startup (Adaptive) JIT Optimizations Profiling of critical hot- spots Interpreter
  118. 118. ©2017 CodeKaram118 The Plot Startup (Adaptive) JIT Optimizations Profiling of critical hot- spots Interpreter
  119. 119. 119 Adaptive Optimization: Dynamic Deoptimization
  120. 120. 120 Dynamic Deoptimization
  121. 121. ©2017 CodeKaram • Oopsie! • dependencies invalidation • classes unloading and redefinition • uncommon path in compiled code • misguided profiled information 121 Dynamic Deoptimization
  122. 122. 122 Advanced Optimization - Vectorization
  123. 123. 123 Vectorization in HotSpot VM??
  124. 124. ©2017 CodeKaram Vectorization 124 • Utilize SIMD (Single Instruction Multiple Data) instructions offered by the processor. • Generate assembly stubs • Get benefits of operating on cache line size data chunks
  125. 125. 125 Advanced Optimization - Auto-Vectorization
  126. 126. 126 Auto-Vectorization in HotSpot VM??
  127. 127. 127 Look Ma, no stubs!!
  128. 128. 128
  129. 129. ©2017 CodeKaram SuperWord Level Parallelism 129 http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
  130. 130. ©2017 CodeKaram SuperWord Level Parallelism 130 • Loop Unrolling • Alignment Analysis • Pre-Optimization • Identifying Adjacent Memory References • Extending the “PackSet” • Combination and SIMD operation http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
  131. 131. 131 That’s all I got! :)
  132. 132. ©2017 CodeKaram Further Reading 132 • 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
  133. 133. Appendix 133
  134. 134. 134 HotSpot’s Runtime Performance Innovations
  135. 135. ©2017 CodeKaram Runtime 135 Responsiveness Throughput Footprint Locking Improvements Compressed Oops Compressed Oops And Class Pointers Class Data Sharing Compressed Class Pointers Code Cache Class Data Sharing
  136. 136. 136 Advanced Optimizations - Compressed Oops + Compressed Class Pointers
  137. 137. ©2017 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 137
  138. 138. 138 Java Object Layout in Memory
  139. 139. ©2017 CodeKaram139 -Tool to analyze Java object layout :) Java Object Layout (JOL)
  140. 140. ©2017 CodeKaram JOL Command Line Options $ java -jar jol-cli.jar Usage: jol-cli.jar <mode> [optional arguments]* Available modes: estimates: Simulate the class layout in different VM modes. externals: Show the object externals: the objects reachable from a given instance. footprint: Estimate the footprint of all objects reachable from a given instance heapdump: Consume the heap dump and estimate the savings in different layout strategies. heapdumpstats: Consume the heap dump and print the most frequent instances. idealpack: Compute the object footprint under different field layout strategies. internals: Show the object internals: field layout and default contents, object header shapes: Dump the object shapes present in JAR files or heap dumps. string-compress: Consume the heap dumps and figures out the savings attainable with compressed strings. 140
  141. 141. ©2017 CodeKaram JOL Command Line Options $ java -jar jol-cli.jar Usage: jol-cli.jar <mode> [optional arguments]* Available modes: estimates: Simulate the class layout in different VM modes. externals: Show the object externals: the objects reachable from a given instance. footprint: Estimate the footprint of all objects reachable from a given instance heapdump: Consume the heap dump and estimate the savings in different layout strategies. heapdumpstats: Consume the heap dump and print the most frequent instances. idealpack: Compute the object footprint under different field layout strategies. internals: Show the object internals: field layout and default contents, object header shapes: Dump the object shapes present in JAR files or heap dumps. string-compress: Consume the heap dumps and figures out the savings attainable with compressed strings. 141
  142. 142. ©2017 CodeKaram Compressed OOPs and Compressed Class Pointers 142 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  143. 143. ©2017 CodeKaram Compressed OOPs and Compressed Class Pointers 143 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  144. 144. 144 A Java Object Header
  145. 145. ©2017 CodeKaram A Java Object Header 145 Header Klass Mark Word Array Length
  146. 146. 146 Java Object Layout Samples - Uncompressed
  147. 147. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 147
  148. 148. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 148 Mark Word
  149. 149. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 149 Klass
  150. 150. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 150
  151. 151. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 151 Array Length
  152. 152. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 152
  153. 153. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 153
  154. 154. 154 Java Object Layout Samples - Compressed
  155. 155. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 155
  156. 156. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 156 Mark Word
  157. 157. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 157
  158. 158. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 158 Klass
  159. 159. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 159
  160. 160. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 160 Array Length
  161. 161. ©2017 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 161
  162. 162. 162 Java Object Layout Header Comparison
  163. 163. ©2017 CodeKaram Compressed vs Uncompressed Compressed: Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total vs Uncompressed: Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 163
  164. 164. ©2017 CodeKaram Compressed vs Uncompressed Compressed: Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total vs Uncompressed: Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 164
  165. 165. ©2017 CodeKaram Compressed OOPs and Compressed Class Pointers 165 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  166. 166. 166 Java Object Layout Samples - UnCompressed
  167. 167. ©2017 CodeKaram JOL: Class java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 167
  168. 168. ©2017 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 168 JOL: Class
  169. 169. ©2017 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 169 JOL: Class
  170. 170. 170 Java Object Layout Samples - Compressed
  171. 171. ©2017 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 171 JOL: Class
  172. 172. ©2017 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 172 JOL: Class
  173. 173. ©2017 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 173 JOL: Class
  174. 174. ©2017 CodeKaram Compressed OOPs and Compressed Class Pointers 174 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift. P P

×