ics201 lecture 10 : introduction to java virtual machine king fahd university of petroleum &...

32
ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Upload: sibyl-gilbert

Post on 12-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

ICS201

Lecture 10 : Introduction to Java Virtual Machine

King Fahd University of Petroleum & MineralsCollege of Computer Science & Engineering

Information & Computer Science Department

Page 2: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Outline

Java Language, Java Virtual Machine and Java Platform

Organization of Java Virtual Machine Garbage Collection Interpreter and Just-In-Time Compiler

Page 3: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Why Java ?

Currently, Java is the most popular language in the world !

Page 4: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Hello.c

gccfor Win

gccfor Mac

gccfor Linux

WindowsX86

MacPowerPC

Hello

Hello

HelloLinux

X86Hello

Classical compilation model

Page 5: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Hello.java javac

WindowsX86

MacPowerPC

Hello

Hello.class

HelloLinux

X86Hello

Hello

Java Compilation Model

Page 6: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

The Big Picture

A.javaA.java B.javaB.java C.javaC.java

Java Compiler

Java Compiler

A.classA.class B.classB.class C.classC.class

Java Virtual MachineJava Virtual Machine

Java LanguageSpecification

Java VirtualMachine

Specification

Page 7: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Java Platforms (1)

A Java Platform consists Java Virtual Machine CPU A set of standard classes API

JVM in all platforms must satisfy JVM spec. Standard classes can be tailored according to the

resource constraints Three levels of Java platforms:

J2EE, J2SE and J2ME

Page 8: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Java Platforms (2)

From KVM White Paper (Sun Microsystem)

Page 9: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

What Is in the JVM Spec?

Bytecodes – the instruction set for Java Virtual Machine

Class File Format – The platform independent representation of Java binary code

Verification Rules – the algorithm for identifying programs that cannot compromise the integrity of the JVM

Page 10: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Organization of JVM

Class AreaClass Area

Class Information

Constant Pool

Method Area

HeapHeap StackStack NativeStack

NativeStack

ExecutionEngine

ExecutionEngine

NativeInterface

NativeInterfaceClass

Loader

ClassLoader

File System*.class

File System*.class

Internet*.class

Internet*.class

PC, FP, SP Registers

NativeMethods

NativeMethods

Page 11: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Class Loader

1. Loading: finding and importing the binary data for a class

2. Linking:• Verification: ensuring the correctness of the imported

type • Preparation: allocating memory for class variables and

initializing the memory to default values • Resolution: transforming symbolic references from the

type into direct references.

3. Initialization: invoking Java code that initializes class variables to their proper starting values

Page 12: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Class Area

Class Information Internal representation of Java classes Information about the superclass and implemented

interfaces Information about the fields and methods

Constant Pool

Method Area Contains the bytecodes of the methods of the loaded

classes

Page 13: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Organization of JVM

Class AreaClass Area

Class Information

Constant Pool

Method Area

HeapHeap StackStack NativeStack

NativeStack

ExecutionEngine

ExecutionEngine

NativeInterface

NativeInterfaceClass

Loader

ClassLoader

File System*.class

File System*.class

Internet*.class

Internet*.class

PC, FP, SP Registers

NativeMethods

NativeMethods

Page 14: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Stack

The Java stack is composed of frames A frame contains the state of one Java method invocation Logically, a frame has two parts: local variable array and

operand stack JVM has no registers; it uses the operand stack for

storage of intermediate data values to keep the JVM's instruction set compact to facilitate implementation on architectures with limited

number of general purpose registers Each Java thread has its own stack and cannot

access the stack of other threads

Page 15: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Stack Frame

public class A { ... ... void f(int x) { int i; for(i=0; i<x; i++) { ... ... } ... ... }

Return PC

Caller’s FP

Caller’s SP

x

i

Inter-Mediate

DataValues

SP

LocalVariable

Array

FP

OperandStack

Page 16: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

a = (b + c) 2

iload_2 (load variable b)

iload_3 (load variable c)

iadd (addition)

iconst_2 (load constant value 2)

imul (multiplication)

istore_1 (save the value of a)

Bytecode

Example

Page 17: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Stack – Each Thread Has its own Stack

Heap

Stack

Thread 1 Thread 2 Thread 3

Frame

Frame

Frame

Frame

Frame

Frame

Frame

Frame

Page 18: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Organization of JVM

Class AreaClass Area

Class Information

Constant Pool

Method Area

HeapHeap StackStack NativeStack

NativeStack

ExecutionEngine

ExecutionEngine

NativeInterface

NativeInterfaceClass

Loader

ClassLoader

File System*.class

File System*.class

Internet*.class

Internet*.class

PC, FP, SP Registers

NativeMethods

NativeMethods

Page 19: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Heap

All Java objects are allocated in the heap

Java applications cannot explicitly free an object

The Garbage Collector is invoked from time to time automatically to reclaim the objects that are no longer needed by the application

The heap is shared by all Java threads

Page 20: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Java Objects in the Heap

Class Area

class A

class Object

Heap

clazzFields 1

Fields 2

… …Fields n

clazz… …

clazz

… …

class B

Page 21: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Garbage Collector

Roots: internally defined by the JVM implementation

root

A B

C D

E F

G

root

A B

C D

E F

G

Live ObjectsLive Objects: reachable from the roots: reachable from the roots

Garbage (Dead Objects)Garbage (Dead Objects): not reachable from the roots, not : not reachable from the roots, not accessible to the applicationaccessible to the application

Page 22: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

rootA

B

C

D

E

F

G

rootA

B

C

D

E

F

G

root

B

E

G

rootB

E

G

Before GC

Live Garbage Unknown Free

After Mark After Sweep After Compact

Mark / Sweep / Compaction

Page 23: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Organization of JVM

Class AreaClass Area

Class Information

Constant Pool

Method Area

HeapHeap StackStack NativeStack

NativeStack

ExecutionEngine

ExecutionEngine

NativeInterface

NativeInterfaceClass

Loader

ClassLoader

File System*.class

File System*.class

Internet*.class

Internet*.class

PC, FP, SP Registers

NativeMethods

NativeMethods

Page 24: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Execution Engine

Executes Java bytecodes either using interpreter or Just-In-Time compiler

Registers: PC: Program Counter FP: Frame Pointer SP: Operand Stack Top Pointer

Page 25: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Interpreter vs Just-In-Time Compiler

CPU

Interpreter

Bytecode

JITCompiler

CPU

NativeCode

Bytecode

Interpretation JIT Compilation

Page 26: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

a = (b + c) 2

iload_2 (load variable b)

iload_3 (load variable c)

iadd (addition)

iconst_2 (load constant value 2)

imul (multiplication)

istore_1 (save the value of a)

Bytecode

Interpretation

Page 27: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Bytecode Interpreter (1)

while(program not end ) { fetch next bytecode => b switch(b) { case ILOAD: load an integer from the local variable array and push on top of current operand stack; case ISTORE: pop an integer from the top of current operand stack and store it into the local variable array; case ALOAD: ... ... } // end of switch} // end of while

Page 28: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Bytecode interpreter (2)

Advantage Ease to implement Does not need extra memory to store compiled code

Disadvantage Very Slow: 10 ~ 100 times slower than execution of native

code

Page 29: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Just-In-Time Compiler

Dynamically compiles bytecode into native code at runtime, usually in method granularity

Execution of native code is much faster than interpretation of bytecode

Compilation is time consuming and may slow down the application

Tradeoffs between execution time and compilation time

Page 30: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

Adaptive Compiler

Observation: less than 20% of the methods account for more than 80% of execution time

Methods contains loop with large number of iteration; Methods that are frequently invoked

Idea 1: only compile the methods where the application spends a lot of time

Idea 2: perform advanced compiler optimization for the hottest methods, simple or no compiler optimization for less hot methods

Page 31: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

How Adaptive Compiler Works

Set three thresholds T1, T2 (T1<T2) Each method has a counter that is initialized to 0.

Whenever the method is invoked, increase its counter by 1 The methods with counter lower than T1 are executed using

interpreter When a method’s counter reaches T1, compile this method

with simple optimizations When a method’s counter reaches T2, recompile this

method with advanced optimizations

Page 32: ICS201 Lecture 10 : Introduction to Java Virtual Machine King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information

The end