1 comp 144 programming language concepts felix hernandez-campos lecture 32: the java virtual machine...

25
COMP 144 Programming Language Concepts Felix Hernandez-Campos 1 Lecture 32: Lecture 32: The Java Virtual Machine The Java Virtual Machine COMP 144 Programming Language COMP 144 Programming Language Concepts Concepts Spring 2002 Spring 2002 Felix Hernandez-Campos Felix Hernandez-Campos April 12 April 12 The University of North Carolina at The University of North Carolina at Chapel Hill Chapel Hill

Post on 20-Dec-2015

223 views

Category:

Documents


6 download

TRANSCRIPT

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

11

Lecture 32:Lecture 32:The Java Virtual MachineThe Java Virtual Machine

COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts

Spring 2002Spring 2002

Felix Hernandez-CamposFelix Hernandez-Campos

April 12April 12

The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

22

The Java Virtual MachineThe Java Virtual Machine

• Java ArchitectureJava Architecture– Java Programming LanguageJava Programming Language– Java Virtual Machine (JVM)Java Virtual Machine (JVM)– Java APIJava 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 representationintermediate program representation

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

33

ReferenceReference

• The content of this lecture is based on The content of this lecture is based on Inside the Inside the Java 2 Virtual MachineJava 2 Virtual Machine by Bill Venners by Bill Venners

– Chapter 1Introduction to Java's ArchitectureChapter 1Introduction to Java's Architecture» http://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitecthttp://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect

urePrint.htmlurePrint.html

– Chapter 5 The Java Virtual MachineChapter 5 The Java Virtual Machine» http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1.http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1.

htmlhtml

– Interactive IllustrationsInteractive Illustrations» http://www.artima.com/insidejvm/applets/index.htmlhttp://www.artima.com/insidejvm/applets/index.html

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

44

The Java Programming EnvironmentThe Java Programming Environment

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

55

The Java PlatformThe Java Platform

• The byte code generated by the Java front-end is an The byte code generated by the Java front-end is an intermediate formintermediate form

– CompactCompact– Platform-independentPlatform-independent

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

66

Phases of CompilationPhases of Compilation

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

77

The Role of the Virtual MachimeThe Role of the Virtual Machime

Local or Local or RemoteRemote

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

88

The Execution EngineThe Execution Engine

• Back-end transformation and executionBack-end transformation and execution– Simple JVM: byte code interpretationSimple JVM: byte code interpretation– Just-in-timeJust-in-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 invokedthey are invoked

» The machine code is cached for subsequent invocationThe machine code is cached for subsequent invocation» It requires more memoryIt requires more memory

– Adaptive optimizationAdaptive optimization» The interpreter monitors the activity of the program, compiling The interpreter monitors the activity of the program, compiling

the heavily used part of the program into machine codethe heavily used part of the program into machine code» It is much faster than simple interpretationIt 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

2020%%//8080%% 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)is responsible for 80% of the execution)

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

99

The Java Virtual MachineThe Java Virtual Machine

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1010

Shared Data AreasShared Data Areas

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1111

Thread Data AreasThread Data Areas

Frame in Frame in ExecutionExecution

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1212

Stack FramesStack Frames

• Stack frames have three parts:Stack frames have three parts:– Local variablesLocal variables– Operand stackOperand stack– Frame dataFrame data

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1313

Stack FrameStack FrameLocal VariablesLocal Variables

class Example3a {class Example3a {

public static int public static int runClassMethodrunClassMethod(int i, long (int 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 int public int runInstanceMethodrunInstanceMethod(char c, (char c, double d, short s, boolean double d, short s, boolean b) {b) {

return 0;return 0;

}}

}}

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1414

Stack FrameStack FrameOperand StackOperand Stack

Adding 2 numbersAdding 2 numbers

iload_0iload_0iload_1iload_1IaddIaddistore_2istore_2

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1515

Execution ModelExecution Model

• Eternal Math ExampleEternal Math Example– http://www.artima.com/insidejvm/applets/EternalMath.hthttp://www.artima.com/insidejvm/applets/EternalMath.ht

mlml

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1616

Stack FrameStack FrameFrame DataFrame Data

• The stack frame also supportsThe stack frame also supports– Constant pool resolutionConstant pool resolution– Normal method returnNormal method return– Exception dispatchException dispatch

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1717

Stack FrameStack FrameFrame Allocation in a HeapFrame Allocation in a Heap

class Example3c {class Example3c {

public static void public static void addAndPrint()addAndPrint() { {

double result = double result = addTwoTypes(1, 88.88);addTwoTypes(1, 88.88);

System.out.println(result)System.out.println(result);;

}}

public static double public static double addTwoTypesaddTwoTypes(int i, double (int i, double d) {d) {

return i + d;return i + d;

}}

}}

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1818

Stack FrameStack FrameNative MethodNative 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 JNIcreated for JNI

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1919

The HeapThe Heap

• Class instances and array are stores in a single, Class instances and array are stores in a single, shared heapshared heap

• Each Java application has its own heapEach Java application has its own heap– IsolationIsolation– But a JVM crash will break this isolationBut a JVM crash will break this isolation

• JVM heaps always implement garbage collection JVM heaps always implement garbage collection mechanismsmechanisms

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2020

HeapHeapMonolithic Object RepresentationMonolithic Object Representation

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2121

The HeapThe HeapSplitted Object RepresentationSplitted Object Representation

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2222

ExampleExample

• HeapOfFishHeapOfFish– http://www.artima.com/insidejvm/applets/HeapOfFish.htmhttp://www.artima.com/insidejvm/applets/HeapOfFish.htm

ll

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2323

The HeapThe HeapMemory/Speed TradeoffMemory/Speed Tradeoff

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2424

The HeapThe HeapArrays as ObjectsArrays as Objects

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2525

Reading AssignmentReading Assignment

• Inside the Java 2 Virtual MachineInside the Java 2 Virtual Machine by Bill Venners by Bill Venners– Ch 1Ch 1– Ch 5Ch 5– IllustrationsIllustrations