Felix Hernandez-Campos
1
COMP 144 Programming Language Concepts Felix Hernandez-Campos
1 1
Lecture 32:
Lecture 32:
The Java Virtual Machine
The Java Virtual Machine
COMP 144 Programming Language Concepts
COMP 144 Programming Language Concepts
Spring 2002
Spring 2002
Felix Hernandez
Felix Hernandez-
-Campos
Campos
April 12
April 12
The University of North Carolina at Chapel Hill
The University of North Carolina at Chapel Hill
COMP 144 Programming Language Concepts Felix Hernandez-Campos
2 2
The Java Virtual Machine
The Java Virtual Machine
•
•
Java Architecture
Java Architecture
–
–
Java Programming Language
Java Programming Language
–
–
Java Virtual Machine (JVM)
Java Virtual Machine (JVM)
–
–
Java API
Java API
•
•
We will use the JVM as a case study of an
We will use the JVM as a case study of an
intermediate program representation
COMP 144 Programming Language Concepts Felix Hernandez-Campos 3 3
Reference
Reference
•
•
The content of this lecture is based on
The content of this lecture is based on
Inside the
Inside the
Java 2 Virtual Machine
Java 2 Virtual Machine
by Bill
by Bill
Venners
Venners
–
–
Chapter 1Introduction to Java's Architecture
Chapter 1Introduction to Java's Architecture
»
»
http://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect
http://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect
urePrint.html
urePrint.html
–
–
Chapter 5 The Java Virtual Machine
Chapter 5 The Java Virtual Machine
»
»
http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1.
http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1.
html
html
–
–
Interactive Illustrations
Interactive Illustrations
»
»
http://www.artima.com/insidejvm/applets/index.html
http://www.artima.com/insidejvm/applets/index.html
The Java Programming Environment
Felix Hernandez-Campos
3
COMP 144 Programming Language Concepts Felix Hernandez-Campos
5 5
The Java Platform
The Java Platform
•
•
The byte code generated by the Java front
The byte code generated by the Java front
-
-
end is an
end is an
intermediate form
intermediate form
–
–
Compact
Compact
–
–
Platform-
Platform
-independent
independent
COMP 144 Programming Language Concepts Felix Hernandez-Campos
6 6
Phases of Compilation
COMP 144 Programming Language Concepts Felix Hernandez-Campos
7 7
The Role of the Virtual
The Role of the Virtual
Machime
Machime
Local or
Local or
Remote
Remote
The Execution Engine
The Execution Engine
•
•
Back
Back
-
-
end transformation and execution
end transformation and execution
–
–
Simple JVM: byte code interpretation
Simple JVM: byte code interpretation
–
–
Just
Just
-
-
in
in
-
-
time
time
compiler
compiler
»
»
Method byte codes are compiled into machine code the first time
Method byte codes are compiled into machine code the first time
they are invoked
they are invoked
»
»
The machine code is cached for subsequent invocation
The machine code is cached for subsequent invocation
»
»
It requires more memory
It requires more memory
–
–
Adaptive optimization
Adaptive optimization
»
»
The interpreter monitors the activity of the program, compiling
The interpreter monitors the activity of the program, compiling
the
the
heavily used part of the program into machine code
heavily used part of the program into machine code
»
»
It is much faster than simple interpretation
It is much faster than simple interpretation
»
»
The memory requirement is only slightly larger due to the
The memory requirement is only slightly larger due to the
20
20
%
%
/80
/
80
%
%
rule of program execution (In general, 20% of the code
rule of program execution (In general, 20% of the code
is responsible for 80% of the execution)
Felix Hernandez-Campos
5
COMP 144 Programming Language Concepts Felix Hernandez-Campos
9 9
The Java Virtual Machine
The Java Virtual Machine
COMP 144 Programming Language Concepts Felix Hernandez-Campos
10 10
Shared Data Areas
COMP 144 Programming Language Concepts Felix Hernandez-Campos
11 11
Thread Data Areas
Thread Data Areas
Frame in
Frame in
Execution
Execution
Stack Frames
Stack Frames
•
•
Stack frames have three parts:
Stack frames have three parts:
–
–
Local variables
Local variables
–
–
Operand stack
Operand stack
–
Felix Hernandez-Campos
7
COMP 144 Programming Language Concepts Felix Hernandez-Campos 13 13
Stack Frame
Stack Frame
Local Variables
Local Variables
class Example3a {
class Example3a {
public static
public static
int
int
runClassMethod
runClassMethod
(
(
int
int
i, long
i, long
l, float f, double d, Object
l, float f, double d, Object
o, byte b) {
o, byte b) {
return 0;
return 0;
}
}
public
public
int
int
runInstanceMethod
runInstanceMethod
(char c,
(char c,
double d, short s, boolean
double d, short s, boolean
b) {
b) {
return 0;
return 0;
}
}
}
}
COMP 144 Programming Language Concepts Felix Hernandez-Campos 14 14
Stack Frame
Stack Frame
Operand Stack
Operand Stack
Adding 2 numbers
Adding 2 numbers
iload
iload_0
_0
iload
iload_1
_1
Iadd
Iadd
istore
istore_2
_2
COMP 144 Programming Language Concepts Felix Hernandez-Campos 15 15
Execution Model
Execution Model
•
•
Eternal Math Example
Eternal Math Example
–
–
http://www.artima.com/insidejvm/applets/EternalMath.htm
http://www.artima.com/insidejvm/applets/EternalMath.htm
l
l
Stack Frame
Stack Frame
Frame Data
Frame Data
•
•
The stack frame also supports
The stack frame also supports
–
–
Constant pool resolution
Constant pool resolution
–
–
Normal method return
Normal method return
–
Felix Hernandez-Campos
9
COMP 144 Programming Language Concepts Felix Hernandez-Campos
17 17
Stack Frame
Stack Frame
Frame Allocation in a Heap
Frame Allocation in a Heap
class Example3c {
class Example3c {
public static void
public static void
addAndPrint
addAndPrint
()
()
{
{
double result =
double result =
addTwoTypes
addTwoTypes
(1, 88.88);
(1, 88.88);
System.out.
System.out.
println
println
(result)
(result)
;
;
}
}
public static double
public static double
addTwoTypes
addTwoTypes
(
(
int
int
i, double
i, double
d) {
d) {
return i + d;
return i + d;
}
}
}
}
COMP 144 Programming Language Concepts Felix Hernandez-Campos 18 18
Stack Frame
Stack Frame
Native Method
Native Method
•
•
A simulated stack of the target language (e.g. C) is
A simulated stack of the target language (e.g. C) is
created for JNI
COMP 144 Programming Language Concepts Felix Hernandez-Campos 19 19
The Heap
The Heap
•
•
Class instances and array are stores in a single,
Class instances and array are stores in a single,
shared heap
shared heap
•
•
Each Java application has its own heap
Each Java application has its own heap
–
–
Isolation
Isolation
–
–
But a JVM crash will break this isolation
But a JVM crash will break this isolation
•
•
JVM heaps always implement garbage collection
JVM heaps always implement garbage collection
mechanisms
mechanisms
Heap
Heap
Monolithic Object Representation
Felix Hernandez-Campos
11
COMP 144 Programming Language Concepts Felix Hernandez-Campos 21 21
The Heap
The Heap
Splitted
Splitted
Object Representation
Object Representation
COMP 144 Programming Language Concepts Felix Hernandez-Campos 22 22
Example
Example
•
•
HeapOfFish
HeapOfFish
–
–
http://www.artima.com/insidejvm/applets/HeapOfFish.htm
http://www.artima.com/insidejvm/applets/HeapOfFish.htm
l
l
COMP 144 Programming Language Concepts Felix Hernandez-Campos 23 23
The Heap
The Heap
Memory/Speed Tradeoff
Memory/Speed Tradeoff
The Heap
The Heap
Arrays as Objects
Arrays as Objects
Felix Hernandez-Campos
13
COMP 144 Programming Language Concepts Felix Hernandez-Campos 25 25