JVM Internals (2015)
-
Upload
luiz-teston -
Category
Technology
-
view
103 -
download
0
Transcript of JVM Internals (2015)
JVM INTERNALSIntroduction to Virtual Machines and the JVM
1
Luiz Testonwww.fracta.cc
This presentation is available online atfracta.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
HOW TO 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 BASED VM
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 BASED VM
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
GCManaging 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• [email protected]• http://fracta.cc• http://linkedin.com/in/teston• http://twitter.com/FeuTeston
KEEP IN TOUCH
Feedback about the presentation appreciated.
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