SlideShare a Scribd company logo
JVM INTERNALS
Introduction toVirtual Machines and the JVM
1
LuizTeston
www.fracta.cc
www.corkjug.com
1st meetup in14/10
corkjug.com
VIRTUAL MACHINES
3
Mimics a Real Machine
4
Loads and execute code
5
CLASSLOADERS
6
Java classes are loaded on demand
7
How they are loaded is up to the classloader
8
9
10
Classloaders are hierarchical
11
12
13
boot jars
LibraryA.jar
LibraryB.jar
14
boot jars
LibraryA.jar
LibraryB.jar
Load class “Main”
15
boot jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
16
boot jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
Not Here
17
boot jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
Not Here
Wait…
18
boot jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
Not Here
Not Here
run time
generated code
19
boot jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
Not Here
Not Here
run time
generated code
Here!
20
HOWTO EXECUTE CODE?
21
INTERPRETING
22
Line of text AST
23
Simple example: sum of two numbers
24
25
int variable: j
26
int variable: jint variable: i
27
int variable: jint variable: i
sum
28
int variable: jint variable: i
sum
return
29
Errors usually are caught at runtime
30
COMPILING
31
Code compiled to binary prior to the execution.
32
Some errors can be caught at compile time
33
Binary can be: native code,VM bytecode and so on…
34
Bytecode: Byte sized OPCODE
35
REGISTER BASEDVM
36
Works like your processor
37
Simple example: summing two numbers
38
R1 R2 RN…
39
R1 R2 RN…
1
40
R1 R2 RN…
1
+2
41
R1 R2 RN…
3
42
STACK BASEDVM
43
Works like you HP48G calculator
44
45
1
46
1
2
47
1
2
+
48
3
49
JVM IS STACK BASED
50
Eventually byte code is compiled
to native code on the fly
51
DYNAMIC MEMORY
52
C/C++ Approach: memory as a big array
53
index size variable
54
index size variable
1 1 i
55
index size variable
1 1 i
2 2 l
56
index size variable
1 1 i
2 2 l
3 4 c
57
index size variable
1 1 i
3 4 c
58
Few caveats for this approach
59
Possible memory fragmentation
60
Possible memory fragmentation
4 sized var doesn’t fit
61
Possible memory leak
62
Possible memory leak
used vars unused vars
63
Possible invalid pointer
64
Possible invalid pointer
variable pointing here
65
JVM based approach: Garbage Collector
66
SIMPLE GC ALGORITHMS
67
MARK AND SWEEP
68
69
VISIBLE REF
70
71
72
COPY
73
74
VISIBLE REF
75
76
77
78
79
IN PRACTICE
80
Eden
Survivor 1
Survivor 2
GC
81
Eden
Survivor 1
Survivor 2
GC
82
Eden
Survivor 1
Survivor 2
Wait…GC
83
Eden
Survivor 1
Survivor 2
off you goGC
84
Eden
Survivor 1
Survivor 2
GC
t
85
Eden
Survivor 1
Survivor 2
GC
t ?
86
Eden
Survivor 1
Survivor 2
GC
t ?
Wait…
87
Eden
Survivor 1
Survivor 2
GC
? t
Wait…
88
Eden
Survivor 1
Survivor 2
GC
? t
off you go
89
Eden
Survivor 1
Survivor 2
GC
? t
t2
90
Eden
Survivor 1
Survivor 2
GC
? t
t2
t is still used within t2
91
Eden
Survivor 1
Survivor 2
GC
? t
t2
? is not used…
92
Eden
Survivor 1
Survivor 2
GC
? t
t2
Whatever, I have plenty of memory
93
Few caveats for this approach
94
GC Wait…
GC needs to be properly configured
95
GC …
GC can be unpredictable
96
GC
No control over memory layout
97
GC
No control over memory layout
Let me do this job!
98
GC
Managing memory
is hard!
99
GC
Having a GC doing the
hard work is good.
100
WHAT ABOUT PERM GEN?
101
102
@Deprecated
HANDS ON
103
• Create a .java file
• Compile it into a .class file
• Analyse its binary content
105
106
107
108
magic number
109
Source code info
(can be removed by
compilation args)
110
Optimisations?
111
Yes, we appended to a string,
but strings are immutable in Java.
112
the addTimes method
Time to take a look on the JVM Assembly code
113
114
115
116
main method
117
arg to int into a variable
118
new SumArg1Arg2Times
119
StringBuilder optimisation
120
121
Constructor
122
can you see the loop?
123
can you see the loop?
124
add method
changing a field
Going further, let’s look the native assembly code
125
• Google for java dissablembler plugin. Install
it (hdis-i386 or hdis-amd64)
• See the native assembly output
• Enjoy analysing it
Jvm internals 2015 - CorkJUG
large iteration count, so
it can be JIT compiled
Jvm internals 2015 - CorkJUG
native code for java.lang.Object
constructor
Jvm internals 2015 - CorkJUG
native code for addTimes
related to the loop within addTimes
native assembly
related bytecode
Jvm internals 2015 - CorkJUG
native code for add method
bytecode for add method
QUESTIONS?
WHO AM I
Luiz Teston
• 15 years on the field mostly as a consultant, working on
non trivial projects using Java, C++ and functional
programming
• lteston@fracta.cc
• http://fracta.cc
• http://linkedin.com/in/teston
• http://twitter.com/FeuTeston
KEEP INTOUCH
Feedback about the presentation appreciated.
lteston@fracta.cc
www.fracta.cc
REFERENCES
• Garbage Collection:Algorithms for Automatic Dynamic Memory Management, by
Richard Jones and Rafael Lins.
• Virtual Machines:Versatile Platforms for Systems and Processes, by Jim Smith and Ravi
Nair.
• https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html
• http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/
• http://docs.oracle.com/javase/specs/jvms/se8/html/index.html
• http://mechanical-sympathy.blogspot.ie/2013/06/printing-generated-assembly-code-
from.html
• http://www.slideshare.net/CharlesNutter/redev-2011-jvm-jit-for-dummies-what-the-jvm-
does-with-your-bytecode-when-youre-not-looking
142

More Related Content

PDF
Jvm internals
PDF
The JVM - Internal ( 스터디 자료 )
PDF
Invokedynamic / JSR-292
PDF
Inside the JVM - Follow the white rabbit! / Breizh JUG
PDF
What your jvm can do for you
PDF
From dot net_to_rails
PDF
Threads and Java Memory Model Explained
PDF
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
Jvm internals
The JVM - Internal ( 스터디 자료 )
Invokedynamic / JSR-292
Inside the JVM - Follow the white rabbit! / Breizh JUG
What your jvm can do for you
From dot net_to_rails
Threads and Java Memory Model Explained
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia

What's hot (20)

PPT
Java Performance Tuning
PDF
Intrinsic Methods in HotSpot VM
PDF
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
PDF
Using Flame Graphs
PDF
スローダウン、ハングを一発解決 スレッドダンプはトラブルシューティングの味方 #wlstudy
KEY
State of the art - server side JavaScript - web-5 2012
PDF
Shark
PDF
Groovy Grails DevJam Jam Session
PDF
State of the art: Server-Side JavaScript - dejeuner fulljs
PDF
JVM JIT-compiler overview @ JavaOne Moscow 2013
PDF
Let'swift "Concurrency in swift"
PDF
Size of in java
KEY
State of the art: server-side javaScript - NantesJS
PPTX
JVM: A Platform for Multiple Languages
PPTX
Metaprogramming Techniques In Groovy And Grails
PDF
A New Age of JVM Garbage Collectors (Clojure Conj 2019)
PDF
Java Performance & Profiling
PPTX
Java Jit. Compilation and optimization by Andrey Kovalenko
PDF
Software Profiling: Understanding Java Performance and how to profile in Java
PDF
Multithreading in Node.js and JavaScript
Java Performance Tuning
Intrinsic Methods in HotSpot VM
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
Using Flame Graphs
スローダウン、ハングを一発解決 スレッドダンプはトラブルシューティングの味方 #wlstudy
State of the art - server side JavaScript - web-5 2012
Shark
Groovy Grails DevJam Jam Session
State of the art: Server-Side JavaScript - dejeuner fulljs
JVM JIT-compiler overview @ JavaOne Moscow 2013
Let'swift "Concurrency in swift"
Size of in java
State of the art: server-side javaScript - NantesJS
JVM: A Platform for Multiple Languages
Metaprogramming Techniques In Groovy And Grails
A New Age of JVM Garbage Collectors (Clojure Conj 2019)
Java Performance & Profiling
Java Jit. Compilation and optimization by Andrey Kovalenko
Software Profiling: Understanding Java Performance and how to profile in Java
Multithreading in Node.js and JavaScript
Ad

Viewers also liked (20)

PDF
Garbage collection in JVM
PDF
Cборка мусора в Java без пауз (HighLoad++ 2013)
PDF
Virtualizing Java in Java (jug.ru)
PDF
Java GC tuning and monitoring (by Alexander Ashitkin)
PDF
JVM Internals - Garbage Collection & Runtime Optimizations
PDF
Tiered Compilation in Hotspot JVM
PDF
JVM Internals - NHJUG Jan 2012
PDF
JIT-компиляция в виртуальной машине Java (HighLoad++ 2013)
PDF
Секреты сборки мусора в Java [DUMP-IT 2012]
PDF
JVM Internals - NEJUG Nov 2010
PDF
Performance Test Driven Development with Oracle Coherence
PDF
JVM Internals (2015)
PDF
Let's talk about Garbage Collection
PPT
Garbage collection in JVM
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
PDF
Understanding JVM
PDF
Inside Android's Dalvik VM - NEJUG Nov 2011
PPTX
Jvm Architecture
PPTX
Java GC
PDF
Understanding Garbage Collection
Garbage collection in JVM
Cборка мусора в Java без пауз (HighLoad++ 2013)
Virtualizing Java in Java (jug.ru)
Java GC tuning and monitoring (by Alexander Ashitkin)
JVM Internals - Garbage Collection & Runtime Optimizations
Tiered Compilation in Hotspot JVM
JVM Internals - NHJUG Jan 2012
JIT-компиляция в виртуальной машине Java (HighLoad++ 2013)
Секреты сборки мусора в Java [DUMP-IT 2012]
JVM Internals - NEJUG Nov 2010
Performance Test Driven Development with Oracle Coherence
JVM Internals (2015)
Let's talk about Garbage Collection
Garbage collection in JVM
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Understanding JVM
Inside Android's Dalvik VM - NEJUG Nov 2011
Jvm Architecture
Java GC
Understanding Garbage Collection
Ad

Similar to Jvm internals 2015 - CorkJUG (20)

PDF
Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...
PDF
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
PDF
The need for speed. What is GraalVM? – 4Developers Wrocław 2019
PDF
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
PDF
How Scala code is expressed in the JVM
PDF
Code lifecycle in the jvm - TopConf Linz
ODP
Double checkedlockingjavasingletons
PDF
Lifecycle of a JIT compiled code
PDF
FTD JVM Internals
PPTX
Fields in Java and Kotlin and what to expect.pptx
PDF
JVM Under The Hood WDI.pdf
PDF
High Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
PDF
Managing JavaScript Complexity
PDF
The Real Thing: Java Virtual Machine
PPT
Jvm performance tuning
PDF
WWCode Dallas - Kubernetes: Learning from Zero to Production
PDF
Fast as C: How to Write Really Terrible Java
PPTX
Multithreading in Scala Native, Scalar.pptx
PPTX
Jvm tuning
PPTX
JS knowing-nuances
Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
The need for speed. What is GraalVM? – 4Developers Wrocław 2019
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
How Scala code is expressed in the JVM
Code lifecycle in the jvm - TopConf Linz
Double checkedlockingjavasingletons
Lifecycle of a JIT compiled code
FTD JVM Internals
Fields in Java and Kotlin and what to expect.pptx
JVM Under The Hood WDI.pdf
High Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
Managing JavaScript Complexity
The Real Thing: Java Virtual Machine
Jvm performance tuning
WWCode Dallas - Kubernetes: Learning from Zero to Production
Fast as C: How to Write Really Terrible Java
Multithreading in Scala Native, Scalar.pptx
Jvm tuning
JS knowing-nuances

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
Teaching material agriculture food technology
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Teaching material agriculture food technology
Understanding_Digital_Forensics_Presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
sap open course for s4hana steps from ECC to s4
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology

Jvm internals 2015 - CorkJUG