Lecture 14 Java Virtual Machine - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Lecture 14 Java Virtual Machine

Description:

Tatung Institute of Technology. 2. 2. Java Program. class SumI ... different applications can not trample on each other's. heap. Heap are shared by all threads. ... – PowerPoint PPT presentation

Number of Views:137
Avg rating:3.0/5.0
Slides: 37
Provided by: johnc69
Category:

less

Transcript and Presenter's Notes

Title: Lecture 14 Java Virtual Machine


1
Lecture 14Java Virtual Machine
  • Instructors
  • Fu-Chiung Cheng
  • (???)
  • Associate Professor
  • Computer Science Engineering
  • Tatung Institute of Technology

1
2
Java Program
  • class SumI
  • public static void main (String args)
  • int count10
  • int sum 0
  • for (int index1indexltcountindex)
  • sumsumindex
  • // method main
  • // class SumI

2
3
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

3
4
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 lt count goto 10
  • 22 return

3
5
Internal Architecture of JVM
Class loader subsystem
class files
method area
heap
Java stacks
pc registers
native method stacks
Runtime data area
Native Method Libraries
Native Method Interface
Execution engine
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 gt 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
object
class data
object
class data
object
class data
object
object
object
class data
class data
object
object
class data
object
Method area
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
Threads Runtime Data Area
thread 1
thread 2
thread 3
thread 1
stack frame
stack frame
stack frame
thread 3
thread 2
stack frame
stack frame
stack frame
thread 3
stack frame
stack frame
stack frame
native method stacks
pc registers
java stacks
11
Threads 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, and initialization 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
    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
  • 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 areas 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
  • types 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, methods 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
    others
  • 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
the object pool
the handle pool
instance data
ptr to object pool
instance data
ptr to class data
instance data
an object reference
instance data
ptr to handle pool
the heap
class data
the method area
  • Splitting an object across a handle pool and
    object pool.

25
ptr to class data
instance data
instance data
an object reference
instance data
ptr to heap
instance data
the heap
class data
The method area
Keeping object data all in one place.
26
prt to class data
prt to class data
length(2)
length(2)
ar00(int)
int ar new int 22
ar0 (ref)
ar01 (int)
ar1 (ref)
prt to class data
ar (an array ref)
length(2)
ar10 (int)
ar11 (int)
the heap
class data for I
class data for I
the method area
One possible heap representation for arrays.
27
prt to special structure
instance data
ptr into heap
instance data
the heap
entry point into all data for the class
prt to full class data
prt to method data
prt to method data
method table
prt to method data
? ? ?
method data
method data
method data
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 objects
    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. methods 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
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()
type
index
type
index
parameter
parameter
0
int i
hidden this
type
reference
0
char c
long
int
1
1
long l
double
double d
3
2
float f
float
double
int
4
short s
4
double d
int
5
boolean b
Object o
6
reference
7
int
byte b
32
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
after iload_0
before starting
after iload_1
after iadd
after istore_2
0
0
0
0
0
100
100
100
100
100
local variables
1
1
1
1
1
98
98
98
98
98
2
2
2
2
2
198
operand stack
100
100
198
98
33
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
34
before invoke addTwoTypes()
After invoke addTwoTypes()
addTwoTypes() returns
0
0
0
1
1
1
1
89.88
88.88

local variables
0
1
frames for addAndPrint( )
1
88.88
frame data
frame for addTwoTypes( )
operand stack
35
After invoke addTwoTypes()
before invoke addTwoTypes()
addTwoTypes() returns
0
0
1
1
frames for addAndPrint( )
0
1
1
1
88.88
88.88
89.88
frame for addTwoTypes( )
36
This C function invokes another C function
this Java method invokes a native method.
stack frame
stack frame
This C function invokes a Java method
stack frame
the current frame
stack frame
a native method stack
Java stacks
Write a Comment
User Comments (0)
About PowerShow.com