Lecture 14 java virtual machine
This presentation is the property of its rightful owner.
Sponsored Links
1 / 36

Lecture 14 Java Virtual Machine PowerPoint PPT Presentation


  • 36 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 14 Java Virtual Machine. Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology. 1. Java Program. class SumI { public static void main (String[] args) { int count=10; int sum =0;

Download Presentation

Lecture 14 Java Virtual Machine

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 14 java virtual machine

Lecture 14Java Virtual Machine

Instructors:

Fu-Chiung Cheng

(鄭福炯)

Associate Professor

Computer Science & Engineering

Tatung Institute of Technology

1


Java program

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

2


Java bytecode

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


Java bytecode1

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

3


Internal architecture of jvm

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


Internal architecture of jvm1

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.


Jvm execution

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.


Runtime data area shared among threads

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


Threads

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.


Thread s runtime data area

Thread’s Runtime Data Area

thread 2

thread 3

thread 1

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


Thread s runtime data area1

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.


Class loader subsystem

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.


Class loader subsystem1

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.


Class loader subsystem2

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


Class loader subsystem3

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.


Class loader subsystem4

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.


Class loader subsystem5

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


Method area

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.)


Type information stored in method area

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


Type information stored in method area1

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


Method table

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.


Lecture 14 java virtual machine

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.


Object representation

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


Lecture 14 java virtual machine

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.


Lecture 14 java virtual machine

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.


Lecture 14 java virtual machine

prt to class data

prt to class data

length=(2)

length=(2)

ar[0][0](int)

int [ ] [ ] ar=

new int [2][2];

ar[0] (ref)

ar[0][1] (int)

ar[1] (ref)

prt to class data

ar (an array ref)

length=(2)

ar[1][0] (int)

ar[1][1] (int)

the heap

class

data for

[[I

class

data for

[I

the method area

One possible heap representation for arrays.


Lecture 14 java virtual machine

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.


Object representation1

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())


Program counter

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.


Java stack

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.


Lecture 14 java virtual machine

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

index

type

parameter

parameter

0

int i

hidden this

type

0

reference

char c

long

1

int

1

long l

double

double d

3

2

float f

float

double

4

short s

int

4

double d

5

boolean b

int

Object o

6

reference

7

int

byte b


Lecture 14 java virtual machine

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


Lecture 14 java virtual machine

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;

}

}


Lecture 14 java virtual machine

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


Lecture 14 java virtual machine

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( )


Lecture 14 java virtual machine

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


  • Login