Upload
galilea-pacer
View
227
Download
0
Embed Size (px)
Citation preview
11
Lecture 14Java Virtual Machine
Instructors:
Fu-Chiung Cheng
(鄭福炯 )
Associate Professor
Computer Science & Engineering
Tatung Institute of Technology
22
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
33
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
43
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
5
Internal Architecture of JVM
Executionengine
Classloader
subsystem
methodarea
heapJava
stackspc
registers
nativemethodstacks
Runtime data area
class files
Native Method
Interface
NativeMethodLibraries
6
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.
7
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.
8
Runtime Data Area Shared among threads
class data
class data
class data
class data
class data
class data
class data
class data class
data
class data
class data
class data
Method area
objectobject objectobject
objectobjectobjectobject
objectobject
objectobject
objectobjectobjectobject
objectobject
heap
9
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, results of intermediate computations.
10
Thread’s Runtime Data Area
thread 1
thread 2
thread 3
pc registers
stack frame
stack frame
stack frame
stack frame
stack frame
stack frame
stack frame
stack frame
thread 1
stack frame
stack frame
stack frame
stack frame
stack frame
stack frame
thread 2
java stacks
stack frame
stack frame
stack frame
stack frame
thread 3
thread 3
native methodstacks
11
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.
12
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 (not middle) of some other instruction.
13
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.
14
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
15
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, andinitialization errors
D. Because Java is multithreaded, initialization of a class or interface requires careful synchronization.
16
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 filewith 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 into JVM.
17
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
18
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.)
19
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 size of local variables, exception table
20
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
21
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.
22
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.
23
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
24
ptr to object pool
ptr to class data
the handle pool
the heap
instance data
instance data
instance data
instance data
the object pool
classdata
the method area
ptr to handle pool
an object reference
• Splitting an object across a handle pool and object pool.
25
ptr to class data
the heap
instance data
instance data
instance data
instance data
The method area
class data
ptr to heap
an object reference
Keeping object data all in one place.
26
prt to class datalength=(2)ar[0] (ref) ar[1] (ref)
the heap
prt to class datalength=(2)
ar[0][0](int) ar[0][1] (int)
prt to class datalength=(2)
ar[1][0] (int)ar[1][1] (int)
class data for
[[I
class data for
[Ithe method area
ar (an array ref)
int [ ] [ ] ar= new int [2][2];
One possible heap representation for arrays.
27
prt to full class dataprt to method dataprt to method dataprt to method data
●●●
entry point into all data for the class
method datamethod
datamethod data
prt to special structureinstance datainstance data
the heap
ptr into heap
method table
the method area
Keeping the method table close at hand.
28
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())
29
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.
30
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 the stack frame is popped and discarded.
31
type
long
float
double
referenceint
typeindex
0
1
3
4
67
parameter
int i
long l
float f
double d
Object obyte b
referenceint
double
intint
typeindex
01
2
45
parameter
hidden thischar c
short sboolean b
double d
runClassMethod() runInstanceMethod()
Class Example3a { // local variable in stack framepublic 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;}}
32
10098
0
12
local variables
operand stack
10098
100
0
1 2
10098
10098
0
1 2
10098
198
0
1 2
10098
198
0
1 2
before starting
after iload_0
after iload_1
after iadd
after istore_2
iload_0 // push local variable 0 (an int) iload_1 // push local variable 1 (an int)iadd // pop two ints, add them and push resultistore_2 // pop int, store into local variable 2
33
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; }}
Java Stack Example
34
1 88.88
01
01
188.88
01
89.88
01
before invoke addTwoTypes()
After invoke addTwoTypes()
addTwoTypes() returns
frames for addAndPrint( )
frame for addTwoTypes( )
local variables
frame data
operand stack
35
01
01
01
before invoke addTwoTypes()
After invoke addTwoTypes()
addTwoTypes() returns
frames for addAndPrint( )
frame for addTwoTypes( )
188.88
188.88 89.88