JVM INTERNALSIntroduction to Virtual Machines and the JVM
1
Luiz Testonwww.fracta.cc
www.corkjug.com
1st meetup in14/10
corkjug.com
VIRTUAL MACHINES
3
Mimics a Real Machine
4
Loads and execute code
5
Java classes are loaded on demand
7
How they are loaded is up to the classloader
8
Classloaders are hierarchical
11
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
HOW TO EXECUTE CODE?
21
Line of text AST
23
Simple example: sum of two numbers
24
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
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 BASED VM
36
Works like your processor
37
Simple example: summing two numbers
38
STACK BASED VM
43
Works like you HP48G calculator
44
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
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
GCManaging memory
is hard!
99
GC Having a GC doing the hard work is good.
100
WHAT ABOUT PERM GEN?
101
• Create a .java file
• Compile it into a .class file
• Analyse its binary content
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
117
arg to int into a variable
118
new SumArg1Arg2Times
119
StringBuilder optimisation
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
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
related bytecode
native code for add method
bytecode for add method
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
• [email protected]• http://fracta.cc• http://linkedin.com/in/teston• http://twitter.com/FeuTeston
KEEP IN TOUCH
Feedback about the presentation appreciated.
[email protected]
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