Transcript Lecture 14

Lecture 14
Java Virtual Machine
Instructors:
Fu-Chiung Cheng
(鄭福炯)
Associate Professor
Computer Science & Engineering
Tatung Institute of Technology
11
Java Program
class SumI {
public static void main (String[] args) {
int count=10;
int sum =0;
for (int index=1;index<count;index++)
sum=sum+index;
} // method main
} // class SumI
22
Java ByteCode
Method void main(java.lang.String[])
0 bipush 10
// byte push 10 to stack (0x10)
2 istore_1
// load 10 (top of stack) to count
3 iconst_0
// push 0 to stack
4 istore_2
// load 0 (top of stack to sum
5 iconst_1
// push 1 to stack
6 istore_3
// load 1 (top of stack) to index
7 goto 17
// go to 17
33
Java ByteCode
10 iload_2
// load sum to stack
11 iload_3// load index to stack
12 iadd
// add
13 istore_2
// store “top of stack” to sum
14 iinc 3 1
// index ++
17 iload_3
// load index to stack
18 iload_1
// load count to stack
19 if_icmplt 10 // if index < count goto 10
22 return
34
Internal Architecture of JVM
class files
method
area
Execution
engine
Class
loader
subsystem
native
Java
pc
heap
method
stacks registers
stacks
Runtime data area
Native
Method
Interface
Native
Method5
Libraries
Internal Architecture of JVM
• Class loader subsystem: a mechanism for loading
classes or interfaces.
• Execution engine: a mechanism for executing the
instructions contained in the methods of loaded classes.
• Runtime data area: a memory to store
bytecodes (method area), objects (heap),
parameters, return values, local variables, (stack)
results of intermediate computations (stack).
• JVM spec. is abstract ==> designers are free to
implement JVM by their own structure.
6
JVM Execution
• JVM execution:
A. class loading,
B. Linking: Verification, Preparation, and Resolution
C. Initialization
D. Executing.
• Each instance of JVM has one method area and one heap
• Method area and heap are shared by all threads.
• JVM parses class and store methods info in method area.
• Objects are put onto heap.
7
Runtime Data Area Shared
among threads
class
data
class
data
class
data
class
data
class
class data
data
Method area
object
object
object
object
object
object
object
object
object
heap
8
Threads
• Java supports multi-thread applications.
• Multi-thread: Processing can be broken into several
separate threads of control which execute at the same time
• A thread is one sequential flow of execution that occurs
at the same time another thread is executing the statement
of the same program
• Each thread has its own PC register (program counter) and
Java Stack.
• PC register: pointer to next instruction to be executed.
• Java Stack: parameters, return values, local variables,
9
results of intermediate computations.
Thread’s Runtime Data Area
thread 1
thread 1 thread 2 thread 3
stack
stack
stack
frame
frame
frame
thread 2
stack
frame
stack
frame
thread 3
stack
frame
stack
frame
stack
frame
pc registers
java stacks
thread 3
stack
frame
native
method
10
stacks
Thread’s Runtime Data Area
• Java Stacks: state of Java method invocation.
• Native method stacks: state of native method invocation.
• Java Stack is composed of stack frames.
• Each stack frame corresponds to one method invocation.
• In the example:
A. Thread 1 and 2 are executing Java methods:
PC registers of Thread 1and 2 are pointed to the next
instruction to be executed
B. Thread 3 is executing a native method:
PC register of Thread 3 is undefined.
11
Class Loader Subsystem
• Class loader subsystem:
A. Loading: find and import the binary data for a type.
B. Linking:
1. Verification: ensure the correctness of imported type.
A. verify .class file is well-formed with a proper
symbol table.
B. verify that bytecode obeys the semantic
requirements of the Java Virtual Machine.
C. Example: VM checks
1. every instruction has a valid operation code
2. Every branch instruction branches to the start
12
(not middle) of some other instruction.
Class Loader Subsystem
B. Linking:
2. Preparation:
A. allocating memory for class variables
and initializing the memory to default values.
B. allocating data structures that are used internally
by the virtual machine:
1. method tables.
2. data structure that allows any method to be
invoked on instances of a class without
requiring a search of superclasses at invocation
time.
13
Class Loader Subsystem
B. Linking:
3. Resolution:
A. transforming symbolic references (e.g. class.field)
into direct references.
B. symbolic reference is replaced with a direct
reference that can be more efficiently processed
if the reference is used repeatedly.
(Quick instructions)
C. Implementation choice:
static linkage vs. laziest resolution
14
Class Loader Subsystem
C. Initialization: invoke java code that initializes class
variables to their proper staring values.
A. execution of any class variable initializers
B. Before a class can be initialized, its direct superclass
must be initialized, as well as the direct superclass
of its direct superclass, and so on, recursively.
C. Initialization may cause loading, linking, and
initialization errors
D. Because Java is multithreaded, initialization of a
class or interface requires careful synchronization.
15
Class Loader Subsystem
• JVM contains two kinds of class loader:
A. Primordial class loader: load trusted class.
It looks in the each directory, in the order the
directories appear in the CLASSPATH, until a file
with appropriate name (filename.class) is found.
B. Class loader objects:
1. Class loader objects(java.lang.ClassLoader) are
part of the Java APIs.
2. Three methods in ClassLoader (defineClass
findSystemClass, resolveClass) are the gateway
16
into JVM.
Class Loader Subsystem
• DefineClass converts an array of bytes into an instance
of class Class.
• Instances of this newly defined class can be created
using the newInstance method in class Class.
• findSystemClass
17
Method Area
• Inside a Java Virtual Machine Instance, information of
about loaded types(classes and interface) are loaded
into a logical area of memory called method area.
A. Class loader reads in the class file (a linear stream of
bytes) and pass it to VM.
B. VM extracts information about the type from the
binary data and stores the information in method area.
PS: Class (static) variables are stored in method area.
• All threads share the Method area. (Thus, access to
the data area’s data structure must be thread-safe.)
18
Type Information stored in
Method Area
• Fully qualified name of the type.
• Fully qualified name of its superclass
• class or interface
• type’s modifier (public, abstract, final)
• List of interface
• Constant pool:
literals, symbolic links to types, fields, methods.
• Field information: field name, type, modifiers
• Method information: name, return type, parameters,
modifiers, method’s bytecodes, size of operand stack
19
size of local variables, exception table
Type Information stored in
Method Area
• Static variables: class variables are shared by all
instances of a class. (must allocate space in method
area before anyone uses it.)
• A reference to class ClassLoader:
for dynamic linking
• A reference to class Class
20
Method Table
• The type information stored in method area must be
organized to be quickly accessible.
• A method table of a class is an array of direct references
to all its methods and the method inherited from its
superclass.
21
Heap
• New objects are allocated by JVM in heap.
• A heap exists in every JVM instance. Thus, two
different applications can not trample on each other’s
heap.
• Heap are shared by all threads. Thus, synchronization
between threads is needed.
• Garbage Collector is responsible for freeing Objects.
• Note objects (in heap) and class (in method area) may
be freed by GC.
22
Object Representation
• JVM spec does not specify how object should be
represented on the heap.
• The data structure of object representation and its GC
may greatly influence performance.
• Three possible object representations:
heap contains two parts: handle pool and object pool
23
the handle pool
an object reference
ptr to object pool
ptr to class data
the object pool
instance data
instance data
instance data
instance data
ptr to handle pool
the heap
class
data
the method area
24
• Splitting an object across a handle pool and object pool.
an object reference
ptr to heap
ptr to class data
instance data
instance data
instance data
instance data
the heap
class
data
The method area
Keeping object data all in one place.
25
prt to class data
length=(2)
int [ ] [ ] ar=
ar[0] (ref)
new int [2][2];
ar[1] (ref)
ar (an array ref)
the heap
class
data for
[[I
prt to class data
length=(2)
ar[0][0](int)
ar[0][1] (int)
prt to class data
length=(2)
ar[1][0] (int)
ar[1][1] (int)
class
data for
[I
the method area
One possible heap representation for arrays.26
prt to special structure
instance data
instance data
ptr into heap
the heap
prt to full class data
prt to method data
prt to method data
method
table prt to method data
●
●
●
the method area
entry point
into all data
for the class
method
method
data
method
data
data
Keeping the method table close at hand.
27
Object Representation
• Each object (instance) of a class has a lock (mutex).
• Only one thread at a time can own the object’s lock.
• All threads that attempt to access to a locked object,
are put into a wait set.
(for wait(), notify() and notifyAll())
28
Program Counter
• Each thread of a running program has its own
pc register (program counter).
• Program counter is created when the thread is started.
• Pc register can point to a native code or bytecode.
29
Java Stack
• When a new method is launched, JVM creates a new
Java stack for the thread.
• A Java stack contains stack frames for method
invocation.
• Java Stack frame:
A. method’s local variables. B. operand stack.
C. frame data (constant pool resolution, return values,
return address, exception dispatch).
• A method can complete itself in either of two ways:
normal and abrupt completion (exception). Either Way
30
the stack frame is popped and discarded.
Class Example3a { // local variable in stack frame
public static int runClassMethod(int i, long l, float f,
double d, Object o, byte b) { return 0;}
public int runInstanceMethod(int i, double d, short s,
boolean b) { return 0;}
}
runInstanceMethod()
runClassMethod()
index
type
parameter
0
type
int i
1
long
long l
3
float
float f
4
double
double d
6 reference Object o
int
7
byte b
index
type
parameter
0 reference hidden this
char c
int
1
2
4
5
double
int
int
double d
short s
boolean b
31
iload_0 // push local variable 0 (an int)
iload_1 // push local variable 1 (an int)
iadd
// pop two ints, add them and push result
istore_2 // pop int, store into local variable 2
before
starting
local 0 100
variables 1 98
2
operand
stack
after
iload_0
after
iload_1
0 100
1 98
2
0 100
1 98
2
0 100
1 98
2
100
100
98
198
after
iadd
after
istore_2
0 100
1 98
2 198
32
Java Stack Example
Class Example3c {
public static void addAndPrint() {
double result = addTwoTypes(1, 88.88);
System.out.println(result);
}
public static double addTwoTypes (int i, double d) {
return i + d;
}
}
33
before invoke
addTwoTypes()
0
1
After invoke
addTwoTypes()
addTwoTypes()
returns
0
1
0
1
1
88.88
frames for
addAndPrint( )
89.88
0
1
1
88.88
local
variables
frame data
frame for
addTwoTypes( )
operand
stack
34
After invoke
addTwoTypes()
before invoke
addTwoTypes()
addTwoTypes()
returns
0
1
1
88.88
0
1
1 88.88
frames for
addAndPrint( )
frame for
addTwoTypes( )
0
1
89.88
35
this Java
method invokes
a native method.
This C function
invokes another
C function
stack
frame
stack
frame
the current
frame
This C function
invokes a Java
method
stack
frame
stack
frame
Java stacks
a native
method
stack
36