SlideShare a Scribd company logo
1 of 126
Download to read offline
JVM INTERNALS
Introduction toVirtual Machines and the JVM
1
LuizTeston
www.fracta.cc
This presentation is available online at
fracta.cc/presentations/jvm
2
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
boor jars
LibraryA.jar
LibraryB.jar
14
boor jars
LibraryA.jar
LibraryB.jar
Load class “Main”
15
boor jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
16
boor jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
Not Here
17
boor jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
Not Here
Wait…
18
boor jars
LibraryA.jar
LibraryB.jar
Load class “Main”
Not Here
Not Here
Not Here
run time
generated code
19
boor 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
Eden
Survivor 1
Survivor 2
GC
67
Eden
Survivor 1
Survivor 2
GC
68
Eden
Survivor 1
Survivor 2
Wait…GC
69
Eden
Survivor 1
Survivor 2
off you goGC
70
Eden
Survivor 1
Survivor 2
GC
t
71
Eden
Survivor 1
Survivor 2
GC
t ?
72
Eden
Survivor 1
Survivor 2
GC
t ?
Wait…
73
Eden
Survivor 1
Survivor 2
GC
? t
Wait…
74
Eden
Survivor 1
Survivor 2
GC
? t
off you go
75
Eden
Survivor 1
Survivor 2
GC
? t
t2
76
Eden
Survivor 1
Survivor 2
GC
? t
t2
t is still used within t2
77
Eden
Survivor 1
Survivor 2
GC
? t
t2
? is not used…
78
Eden
Survivor 1
Survivor 2
GC
? t
t2
Whatever, I have plenty of memory
79
Few caveats for this approach
80
GC Wait…
GC needs to be properly configured
81
GC …
GC can be unpredictable
82
GC
No control over memory layout
83
GC
No control over memory layout
Let me do this job!
84
GC
Managing memory
is hard!
85
GC
Having a GC doing the
hard work is good.
86
HANDS ON
87
• Create a .java file
• Compile it into a .class file
• Analyse its binary content
89
90
91
92
magic number
93
Source code info
(can be removed by
compilation args)
94
Optimisations?
95
Yes, we appended to a string,
but strings are immutable in Java.
96
the addTimes method
Time to take a look on the JVM Assembly code
97
98
99
100
main method
101
arg to int into a variable
102
new SumArg1Arg2Times
103
StringBuilder optimisation
104
105
Constructor
106
can you see the loop?
107
can you see the loop?
108
add method
changing a field
Going further, let’s look the native assembly code
109
• Google for java dissablembler plugin. Install
it (hdis-i386 or hdis-amd64)
• See the native assembly output
• Enjoy analysing it
large iteration count, so
it can be JIT compiled
native code for java.lang.Object
constructor
native code for addTimes
related to the loop within addTimes
native assembly
related bytecode
native code for add method
bytecode for add method
QUESTIONS?
WHO AM I
Luiz Teston
Principle Engineer, CTO at FRACTA
• 15 years on the field, 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
126

More Related Content

What's hot

JVM and Garbage Collection Tuning
JVM and Garbage Collection TuningJVM and Garbage Collection Tuning
JVM and Garbage Collection Tuning
Kai Koenig
 
Jvm operation casual talks
Jvm operation casual talksJvm operation casual talks
Jvm operation casual talks
Yusaku Watanabe
 
Inside The Java Virtual Machine
Inside The Java Virtual MachineInside The Java Virtual Machine
Inside The Java Virtual Machine
elliando dias
 
Java in flames
Java in flamesJava in flames
Java in flames
Isuru Perera
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
Vladimir Ivanov
 

What's hot (20)

JVM and Garbage Collection Tuning
JVM and Garbage Collection TuningJVM and Garbage Collection Tuning
JVM and Garbage Collection Tuning
 
Java performance tuning
Java performance tuningJava performance tuning
Java performance tuning
 
Using Flame Graphs
Using Flame GraphsUsing Flame Graphs
Using Flame Graphs
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Jvm operation casual talks
Jvm operation casual talksJvm operation casual talks
Jvm operation casual talks
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Inside The Java Virtual Machine
Inside The Java Virtual MachineInside The Java Virtual Machine
Inside The Java Virtual Machine
 
Find bottleneck and tuning in Java Application
Find bottleneck and tuning in Java ApplicationFind bottleneck and tuning in Java Application
Find bottleneck and tuning in Java Application
 
자바 성능 강의
자바 성능 강의자바 성능 강의
자바 성능 강의
 
Java Performance & Profiling
Java Performance & ProfilingJava Performance & Profiling
Java Performance & Profiling
 
Fight with Metaspace OOM
Fight with Metaspace OOMFight with Metaspace OOM
Fight with Metaspace OOM
 
Tools for Metaspace
Tools for MetaspaceTools for Metaspace
Tools for Metaspace
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 
The JVM is your friend
The JVM is your friendThe JVM is your friend
The JVM is your friend
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VM
 
Java Performance and Using Java Flight Recorder
Java Performance and Using Java Flight RecorderJava Performance and Using Java Flight Recorder
Java Performance and Using Java Flight Recorder
 
Garbage First and you
Garbage First and youGarbage First and you
Garbage First and you
 
Java in flames
Java in flamesJava in flames
Java in flames
 
Software Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and FlamegraphsSoftware Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and Flamegraphs
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
 

Viewers also liked

Viewers also liked (20)

Java Is Not Dead - Bob McWhirter
Java Is Not Dead - Bob McWhirterJava Is Not Dead - Bob McWhirter
Java Is Not Dead - Bob McWhirter
 
What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
55 New Features in Java SE 8
55 New Features in Java SE 855 New Features in Java SE 8
55 New Features in Java SE 8
 
Administración de memoria en java
Administración de memoria en javaAdministración de memoria en java
Administración de memoria en java
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
OCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsOCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertions
 
Gestión de proyectos tic
Gestión de proyectos ticGestión de proyectos tic
Gestión de proyectos tic
 
Simple IPv4_Subnetting
Simple IPv4_SubnettingSimple IPv4_Subnetting
Simple IPv4_Subnetting
 
El sector de las tic en españa santander 2013 - ericsson
El sector de las tic en españa   santander 2013 - ericssonEl sector de las tic en españa   santander 2013 - ericsson
El sector de las tic en españa santander 2013 - ericsson
 
I pv4 subnetting
I pv4 subnettingI pv4 subnetting
I pv4 subnetting
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Book Preview: Oracle Certified Professional Java (OCP Java) SE 8 Programmer E...
Book Preview: Oracle Certified Professional Java (OCP Java) SE 8 Programmer E...Book Preview: Oracle Certified Professional Java (OCP Java) SE 8 Programmer E...
Book Preview: Oracle Certified Professional Java (OCP Java) SE 8 Programmer E...
 
Rundeck's History and Future
Rundeck's History and FutureRundeck's History and Future
Rundeck's History and Future
 
How do I do DevOps when all I have is Ops?
How do I do DevOps when all I have is Ops?How do I do DevOps when all I have is Ops?
How do I do DevOps when all I have is Ops?
 
Building Python Development Station
Building Python Development StationBuilding Python Development Station
Building Python Development Station
 
Cracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 ExamsCracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 Exams
 
How will DevOps benefit enterprise?
How will DevOps benefit enterprise? How will DevOps benefit enterprise?
How will DevOps benefit enterprise?
 
Opendaylight SDN Controller
Opendaylight SDN ControllerOpendaylight SDN Controller
Opendaylight SDN Controller
 

Similar to JVM Internals (2015)

Similar to JVM Internals (2015) (20)

Jvm internals 2015 - CorkJUG
Jvm internals 2015 - CorkJUGJvm internals 2015 - CorkJUG
Jvm internals 2015 - CorkJUG
 
The need for speed. What is GraalVM? – 4Developers Wrocław 2019
The need for speed. What is GraalVM? – 4Developers Wrocław 2019The need for speed. What is GraalVM? – 4Developers Wrocław 2019
The need for speed. What is GraalVM? – 4Developers Wrocław 2019
 
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
Scala e xchange 2013 haoyi li on metascala a tiny diy jvmScala e xchange 2013 haoyi li on metascala a tiny diy jvm
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
 
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
 
How Scala code is expressed in the JVM
How Scala code is expressed in the JVMHow Scala code is expressed in the JVM
How Scala code is expressed in the JVM
 
Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...
Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...
Devoxx Fr 2022 - Remèdes aux oomkill, warm-ups, et lenteurs pour des conteneu...
 
The JVM - Internal ( 스터디 자료 )
The JVM - Internal ( 스터디 자료 )The JVM - Internal ( 스터디 자료 )
The JVM - Internal ( 스터디 자료 )
 
High Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
High Availability with Galera Cluster - SkySQL Road Show 2013 in BerlinHigh Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
High Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
 
Lifecycle of a JIT compiled code
Lifecycle of a JIT compiled codeLifecycle of a JIT compiled code
Lifecycle of a JIT compiled code
 
FTD JVM Internals
FTD JVM InternalsFTD JVM Internals
FTD JVM Internals
 
The Real Thing: Java Virtual Machine
The Real Thing: Java Virtual MachineThe Real Thing: Java Virtual Machine
The Real Thing: Java Virtual Machine
 
Double checkedlockingjavasingletons
Double checkedlockingjavasingletonsDouble checkedlockingjavasingletons
Double checkedlockingjavasingletons
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Code lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf LinzCode lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf Linz
 
JVM Under The Hood WDI.pdf
JVM Under The Hood WDI.pdfJVM Under The Hood WDI.pdf
JVM Under The Hood WDI.pdf
 
Size of in java
Size of in javaSize of in java
Size of in java
 
How the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java CodeHow the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java Code
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible Java
 
Managing JavaScript Complexity
Managing JavaScript ComplexityManaging JavaScript Complexity
Managing JavaScript Complexity
 
WWCode Dallas - Kubernetes: Learning from Zero to Production
WWCode Dallas - Kubernetes: Learning from Zero to ProductionWWCode Dallas - Kubernetes: Learning from Zero to Production
WWCode Dallas - Kubernetes: Learning from Zero to Production
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

JVM Internals (2015)