COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 1
COMP 144 Programming Language Concepts
Felix 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 HernandezFelix Hernandez--CamposCampos
April 12April 12
The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill
COMP 144 Programming Language Concepts
Felix 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 anWe will use the JVM as a case study of an
intermediate program representationintermediate program representation
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 2
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
33
ReferenceReference
•• The content of this lecture is based onThe content of this lecture is based on Inside theInside the
Java 2 Virtual MachineJava 2 Virtual Machine by Billby Bill VennersVenners
–– 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 Concepts
Felix Hernandez-Campos
44
The Java Programming EnvironmentThe Java Programming Environment
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 3
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
55
The Java PlatformThe Java Platform
•• The byte code generated by the Java frontThe byte code generated by the Java front--end is anend is an
intermediate formintermediate form
–– CompactCompact
–– PlatformPlatform--independentindependent
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
66
Phases of CompilationPhases of Compilation
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 4
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
77
The Role of the VirtualThe Role of the Virtual MachimeMachime
Local orLocal or
RemoteRemote
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
88
The Execution EngineThe Execution Engine
•• BackBack--end transformation and executionend transformation and execution
–– Simple JVM: byte code interpretationSimple JVM: byte code interpretation
–– JustJust--inin--timetime compilercompiler
»» Method byte codes are compiled into machine code the first timeMethod 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, compilingThe interpreter monitors the activity of the program, compiling thethe
heavily used part of the program into machine codeheavily 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 theThe memory requirement is only slightly larger due to the
2020%%//8080%% rule of program execution (In general, 20% of the coderule 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 Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 5
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
99
The Java Virtual MachineThe Java Virtual Machine
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1010
Shared Data AreasShared Data Areas
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 6
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1111
Thread Data AreasThread Data Areas
Frame inFrame in
ExecutionExecution
COMP 144 Programming Language Concepts
Felix 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 Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 7
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1313
Stack FrameStack Frame
Local VariablesLocal Variables
class Example3a {class Example3a {
public staticpublic static intint
runClassMethodrunClassMethod((intint i, longi, long
l, float f, double d, Objectl, float f, double d, Object
o, byte b) {o, byte b) {
return 0;return 0;
}}
publicpublic intint
runInstanceMethodrunInstanceMethod(char c,(char c,
double d, short s, booleandouble d, short s, boolean
b) {b) {
return 0;return 0;
}}
}}
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1414
Stack FrameStack Frame
Operand StackOperand Stack
Adding 2 numbersAdding 2 numbers
iloadiload_0_0
iloadiload_1_1
IaddIadd
istoreistore_2_2
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 8
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1515
Execution ModelExecution Model
•• Eternal Math ExampleEternal Math Example
–– http://www.artima.com/insidejvm/applets/EternalMath.htmhttp://www.artima.com/insidejvm/applets/EternalMath.htm
ll
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1616
Stack FrameStack Frame
Frame 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 Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 9
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1717
Stack FrameStack Frame
Frame Allocation in a HeapFrame Allocation in a Heap
class Example3c {class Example3c {
public static voidpublic static void
addAndPrintaddAndPrint()() {{
double result =double result =
addTwoTypesaddTwoTypes(1, 88.88);(1, 88.88);
System.out.System.out.printlnprintln(result)(result)
;;
}}
public static doublepublic static double
addTwoTypesaddTwoTypes((intint i, doublei, double
d) {d) {
return i + d;return i + d;
}}
}}
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
1818
Stack FrameStack Frame
Native MethodNative Method
•• A simulated stack of the target language (e.g. C) isA simulated stack of the target language (e.g. C) is
created for JNIcreated for JNI
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 10
COMP 144 Programming Language Concepts
Felix 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 collectionJVM heaps always implement garbage collection
mechanismsmechanisms
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
2020
HeapHeap
Monolithic Object RepresentationMonolithic Object Representation
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 11
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
2121
The HeapThe Heap
SplittedSplitted Object RepresentationObject Representation
COMP 144 Programming Language Concepts
Felix 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 Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 12
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
2323
The HeapThe Heap
Memory/Speed TradeoffMemory/Speed Tradeoff
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
2424
The HeapThe Heap
Arrays as ObjectsArrays as Objects
COMP 144
Programming Language Concepts
Lecture 32: The Java Virtual Machine
April 12, 2002
Felix Hernandez-Campos 13
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
2525
Reading AssignmentReading Assignment
•• Inside the Java 2 Virtual MachineInside the Java 2 Virtual Machine by Billby Bill VennersVenners
–– Ch 1Ch 1
–– Ch 5Ch 5
–– IllustrationsIllustrations

Understanding jvm

  • 1.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 1 COMP 144 Programming Language Concepts Felix 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 HernandezFelix Hernandez--CamposCampos April 12April 12 The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill COMP 144 Programming Language Concepts Felix 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 anWe will use the JVM as a case study of an intermediate program representationintermediate program representation
  • 2.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 2 COMP 144 Programming Language Concepts Felix Hernandez-Campos 33 ReferenceReference •• The content of this lecture is based onThe content of this lecture is based on Inside theInside the Java 2 Virtual MachineJava 2 Virtual Machine by Billby Bill VennersVenners –– 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 Concepts Felix Hernandez-Campos 44 The Java Programming EnvironmentThe Java Programming Environment
  • 3.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 3 COMP 144 Programming Language Concepts Felix Hernandez-Campos 55 The Java PlatformThe Java Platform •• The byte code generated by the Java frontThe byte code generated by the Java front--end is anend is an intermediate formintermediate form –– CompactCompact –– PlatformPlatform--independentindependent COMP 144 Programming Language Concepts Felix Hernandez-Campos 66 Phases of CompilationPhases of Compilation
  • 4.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 4 COMP 144 Programming Language Concepts Felix Hernandez-Campos 77 The Role of the VirtualThe Role of the Virtual MachimeMachime Local orLocal or RemoteRemote COMP 144 Programming Language Concepts Felix Hernandez-Campos 88 The Execution EngineThe Execution Engine •• BackBack--end transformation and executionend transformation and execution –– Simple JVM: byte code interpretationSimple JVM: byte code interpretation –– JustJust--inin--timetime compilercompiler »» Method byte codes are compiled into machine code the first timeMethod 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, compilingThe interpreter monitors the activity of the program, compiling thethe heavily used part of the program into machine codeheavily 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 theThe memory requirement is only slightly larger due to the 2020%%//8080%% rule of program execution (In general, 20% of the coderule of program execution (In general, 20% of the code is responsible for 80% of the execution)is responsible for 80% of the execution)
  • 5.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 5 COMP 144 Programming Language Concepts Felix Hernandez-Campos 99 The Java Virtual MachineThe Java Virtual Machine COMP 144 Programming Language Concepts Felix Hernandez-Campos 1010 Shared Data AreasShared Data Areas
  • 6.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos 1111 Thread Data AreasThread Data Areas Frame inFrame in ExecutionExecution COMP 144 Programming Language Concepts Felix 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
  • 7.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 7 COMP 144 Programming Language Concepts Felix Hernandez-Campos 1313 Stack FrameStack Frame Local VariablesLocal Variables class Example3a {class Example3a { public staticpublic static intint runClassMethodrunClassMethod((intint i, longi, long l, float f, double d, Objectl, float f, double d, Object o, byte b) {o, byte b) { return 0;return 0; }} publicpublic intint runInstanceMethodrunInstanceMethod(char c,(char c, double d, short s, booleandouble d, short s, boolean b) {b) { return 0;return 0; }} }} COMP 144 Programming Language Concepts Felix Hernandez-Campos 1414 Stack FrameStack Frame Operand StackOperand Stack Adding 2 numbersAdding 2 numbers iloadiload_0_0 iloadiload_1_1 IaddIadd istoreistore_2_2
  • 8.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 8 COMP 144 Programming Language Concepts Felix Hernandez-Campos 1515 Execution ModelExecution Model •• Eternal Math ExampleEternal Math Example –– http://www.artima.com/insidejvm/applets/EternalMath.htmhttp://www.artima.com/insidejvm/applets/EternalMath.htm ll COMP 144 Programming Language Concepts Felix Hernandez-Campos 1616 Stack FrameStack Frame Frame DataFrame Data •• The stack frame also supportsThe stack frame also supports –– Constant pool resolutionConstant pool resolution –– Normal method returnNormal method return –– Exception dispatchException dispatch
  • 9.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 9 COMP 144 Programming Language Concepts Felix Hernandez-Campos 1717 Stack FrameStack Frame Frame Allocation in a HeapFrame Allocation in a Heap class Example3c {class Example3c { public static voidpublic static void addAndPrintaddAndPrint()() {{ double result =double result = addTwoTypesaddTwoTypes(1, 88.88);(1, 88.88); System.out.System.out.printlnprintln(result)(result) ;; }} public static doublepublic static double addTwoTypesaddTwoTypes((intint i, doublei, double d) {d) { return i + d;return i + d; }} }} COMP 144 Programming Language Concepts Felix Hernandez-Campos 1818 Stack FrameStack Frame Native MethodNative Method •• A simulated stack of the target language (e.g. C) isA simulated stack of the target language (e.g. C) is created for JNIcreated for JNI
  • 10.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 10 COMP 144 Programming Language Concepts Felix 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 collectionJVM heaps always implement garbage collection mechanismsmechanisms COMP 144 Programming Language Concepts Felix Hernandez-Campos 2020 HeapHeap Monolithic Object RepresentationMonolithic Object Representation
  • 11.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 11 COMP 144 Programming Language Concepts Felix Hernandez-Campos 2121 The HeapThe Heap SplittedSplitted Object RepresentationObject Representation COMP 144 Programming Language Concepts Felix Hernandez-Campos 2222 ExampleExample •• HeapOfFishHeapOfFish –– http://www.artima.com/insidejvm/applets/HeapOfFish.htmhttp://www.artima.com/insidejvm/applets/HeapOfFish.htm ll
  • 12.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 12 COMP 144 Programming Language Concepts Felix Hernandez-Campos 2323 The HeapThe Heap Memory/Speed TradeoffMemory/Speed Tradeoff COMP 144 Programming Language Concepts Felix Hernandez-Campos 2424 The HeapThe Heap Arrays as ObjectsArrays as Objects
  • 13.
    COMP 144 Programming LanguageConcepts Lecture 32: The Java Virtual Machine April 12, 2002 Felix Hernandez-Campos 13 COMP 144 Programming Language Concepts Felix Hernandez-Campos 2525 Reading AssignmentReading Assignment •• Inside the Java 2 Virtual MachineInside the Java 2 Virtual Machine by Billby Bill VennersVenners –– Ch 1Ch 1 –– Ch 5Ch 5 –– IllustrationsIllustrations