• No results found

Lecture 32: The Java Virtual Machine. The Java Virtual Machine

N/A
N/A
Protected

Academic year: 2021

Share "Lecture 32: The Java Virtual Machine. The Java Virtual Machine"

Copied!
13
0
0

Loading.... (view fulltext now)

Full text

(1)

Felix Hernandez-Campos

1

COMP 144 Programming Language Concepts Felix Hernandez-Campos

1 1

Lecture 32:

Lecture 32:

The Java Virtual Machine

The Java Virtual Machine

COMP 144 Programming Language Concepts

COMP 144 Programming Language Concepts

Spring 2002

Spring 2002

Felix Hernandez

Felix Hernandez-

-Campos

Campos

April 12

April 12

The University of North Carolina at Chapel Hill

The University of North Carolina at Chapel Hill

COMP 144 Programming Language Concepts Felix Hernandez-Campos

2 2

The Java Virtual Machine

The Java Virtual Machine

Java Architecture

Java Architecture

Java Programming Language

Java Programming Language

Java Virtual Machine (JVM)

Java Virtual Machine (JVM)

Java API

Java API

We will use the JVM as a case study of an

We will use the JVM as a case study of an

intermediate program representation

(2)

COMP 144 Programming Language Concepts Felix Hernandez-Campos 3 3

Reference

Reference

The content of this lecture is based on

The content of this lecture is based on

Inside the

Inside the

Java 2 Virtual Machine

Java 2 Virtual Machine

by Bill

by Bill

Venners

Venners

Chapter 1Introduction to Java's Architecture

Chapter 1Introduction to Java's Architecture

»

»

http://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect

http://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect

urePrint.html

urePrint.html

Chapter 5 The Java Virtual Machine

Chapter 5 The Java Virtual Machine

»

»

http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1.

http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1.

html

html

Interactive Illustrations

Interactive Illustrations

»

»

http://www.artima.com/insidejvm/applets/index.html

http://www.artima.com/insidejvm/applets/index.html

The Java Programming Environment

(3)

Felix Hernandez-Campos

3

COMP 144 Programming Language Concepts Felix Hernandez-Campos

5 5

The Java Platform

The Java Platform

The byte code generated by the Java front

The byte code generated by the Java front

-

-

end is an

end is an

intermediate form

intermediate form

Compact

Compact

Platform-

Platform

-independent

independent

COMP 144 Programming Language Concepts Felix Hernandez-Campos

6 6

Phases of Compilation

(4)

COMP 144 Programming Language Concepts Felix Hernandez-Campos

7 7

The Role of the Virtual

The Role of the Virtual

Machime

Machime

Local or

Local or

Remote

Remote

The Execution Engine

The Execution Engine

Back

Back

-

-

end transformation and execution

end transformation and execution

Simple JVM: byte code interpretation

Simple JVM: byte code interpretation

Just

Just

-

-

in

in

-

-

time

time

compiler

compiler

»

»

Method byte codes are compiled into machine code the first time

Method byte codes are compiled into machine code the first time

they are invoked

they are invoked

»

»

The machine code is cached for subsequent invocation

The machine code is cached for subsequent invocation

»

»

It requires more memory

It requires more memory

Adaptive optimization

Adaptive optimization

»

»

The interpreter monitors the activity of the program, compiling

The interpreter monitors the activity of the program, compiling

the

the

heavily used part of the program into machine code

heavily used part of the program into machine code

»

»

It is much faster than simple interpretation

It is much faster than simple interpretation

»

»

The memory requirement is only slightly larger due to the

The memory requirement is only slightly larger due to the

20

20

%

%

/80

/

80

%

%

rule of program execution (In general, 20% of the code

rule of program execution (In general, 20% of the code

is responsible for 80% of the execution)

(5)

Felix Hernandez-Campos

5

COMP 144 Programming Language Concepts Felix Hernandez-Campos

9 9

The Java Virtual Machine

The Java Virtual Machine

COMP 144 Programming Language Concepts Felix Hernandez-Campos

10 10

Shared Data Areas

(6)

COMP 144 Programming Language Concepts Felix Hernandez-Campos

11 11

Thread Data Areas

Thread Data Areas

Frame in

Frame in

Execution

Execution

Stack Frames

Stack Frames

Stack frames have three parts:

Stack frames have three parts:

Local variables

Local variables

Operand stack

Operand stack

(7)

Felix Hernandez-Campos

7

COMP 144 Programming Language Concepts Felix Hernandez-Campos 13 13

Stack Frame

Stack Frame

Local Variables

Local Variables

class Example3a {

class Example3a {

public static

public static

int

int

runClassMethod

runClassMethod

(

(

int

int

i, long

i, long

l, float f, double d, Object

l, float f, double d, Object

o, byte b) {

o, byte b) {

return 0;

return 0;

}

}

public

public

int

int

runInstanceMethod

runInstanceMethod

(char c,

(char c,

double d, short s, boolean

double d, short s, boolean

b) {

b) {

return 0;

return 0;

}

}

}

}

COMP 144 Programming Language Concepts Felix Hernandez-Campos 14 14

Stack Frame

Stack Frame

Operand Stack

Operand Stack

Adding 2 numbers

Adding 2 numbers

iload

iload_0

_0

iload

iload_1

_1

Iadd

Iadd

istore

istore_2

_2

(8)

COMP 144 Programming Language Concepts Felix Hernandez-Campos 15 15

Execution Model

Execution Model

Eternal Math Example

Eternal Math Example

http://www.artima.com/insidejvm/applets/EternalMath.htm

http://www.artima.com/insidejvm/applets/EternalMath.htm

l

l

Stack Frame

Stack Frame

Frame Data

Frame Data

The stack frame also supports

The stack frame also supports

Constant pool resolution

Constant pool resolution

Normal method return

Normal method return

(9)

Felix Hernandez-Campos

9

COMP 144 Programming Language Concepts Felix Hernandez-Campos

17 17

Stack Frame

Stack Frame

Frame Allocation in a Heap

Frame Allocation in a Heap

class Example3c {

class Example3c {

public static void

public static void

addAndPrint

addAndPrint

()

()

{

{

double result =

double result =

addTwoTypes

addTwoTypes

(1, 88.88);

(1, 88.88);

System.out.

System.out.

println

println

(result)

(result)

;

;

}

}

public static double

public static double

addTwoTypes

addTwoTypes

(

(

int

int

i, double

i, double

d) {

d) {

return i + d;

return i + d;

}

}

}

}

COMP 144 Programming Language Concepts Felix Hernandez-Campos 18 18

Stack Frame

Stack Frame

Native Method

Native Method

A simulated stack of the target language (e.g. C) is

A simulated stack of the target language (e.g. C) is

created for JNI

(10)

COMP 144 Programming Language Concepts Felix Hernandez-Campos 19 19

The Heap

The Heap

Class instances and array are stores in a single,

Class instances and array are stores in a single,

shared heap

shared heap

Each Java application has its own heap

Each Java application has its own heap

Isolation

Isolation

But a JVM crash will break this isolation

But a JVM crash will break this isolation

JVM heaps always implement garbage collection

JVM heaps always implement garbage collection

mechanisms

mechanisms

Heap

Heap

Monolithic Object Representation

(11)

Felix Hernandez-Campos

11

COMP 144 Programming Language Concepts Felix Hernandez-Campos 21 21

The Heap

The Heap

Splitted

Splitted

Object Representation

Object Representation

COMP 144 Programming Language Concepts Felix Hernandez-Campos 22 22

Example

Example

HeapOfFish

HeapOfFish

http://www.artima.com/insidejvm/applets/HeapOfFish.htm

http://www.artima.com/insidejvm/applets/HeapOfFish.htm

l

l

(12)

COMP 144 Programming Language Concepts Felix Hernandez-Campos 23 23

The Heap

The Heap

Memory/Speed Tradeoff

Memory/Speed Tradeoff

The Heap

The Heap

Arrays as Objects

Arrays as Objects

(13)

Felix Hernandez-Campos

13

COMP 144 Programming Language Concepts Felix Hernandez-Campos 25 25

Reading Assignment

Reading Assignment

Inside the Java 2 Virtual Machine

Inside the Java 2 Virtual Machine

by Bill

by Bill

Venners

Venners

Ch 1

Ch 1

Ch 5

Ch 5

Illustrations

Illustrations

References

Related documents

The mission of the EASTC is to promote the production and use of high quality data for ev- idence-based decision making by training producers and users of statistics and providing

Instructional Practices: Teacher led instruction Student created composing Listening and evaluating of professional and peer created selections utilizing MIDI Peer

Rizikos valdymo procesui atliekant operatyvinius veiksmus didelæ átakà turi kitas ope- ratyviniø veiksmø taktikos elementas – operatyvinio taktinio sprendimo numatytiems uþdavi-

Critical Success Factors for Improving Project Management Efficiency and Reducing Project Completion Time A more efficient project management process will help to reduce

Results in Table V also show that maximum distributor shares are lower when movies are shown in older and in larger theaters, in terms of number of screens, and in those theaters

Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] Langevin, Melinda tranquil, tropical room Lasso selection tool

Also Available: Free Online Course How to Program Your Mind For Wealth Click here: MindPowerNews.com/Wealth... & A Native American elder once described his own inner struggles

Results: We provide evidence that exposure of monocyte-derived dendritic cells (MDDCs) to recombinant HIV-1 R5 gp120, but not to CCR5 natural ligand CCL4, influences the expression of