1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 32: The Java Virtual Machine COMP 144 Programming Language Concepts Spring 2002.

Download 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 32: The Java Virtual Machine COMP 144 Programming Language Concepts Spring 2002.

Post on 20-Dec-2015

218 views

Category:

Documents

6 download

TRANSCRIPT

  • Slide 1
  • 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 32: The Java Virtual Machine COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 12 The University of North Carolina at Chapel Hill
  • Slide 2
  • 2 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Java Virtual Machine Java ArchitectureJava Architecture Java Programming Language Java Virtual Machine (JVM) Java API We will use the JVM as a case study of an intermediate program representationWe will use the JVM as a case study of an intermediate program representation
  • Slide 3
  • 3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reference The content of this lecture is based on Inside the Java 2 Virtual Machine by Bill VennersThe content of this lecture is based on Inside the Java 2 Virtual Machine by Bill Venners Chapter 1Introduction to Java's Architecture http://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect urePrint.html http://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect urePrint.htmlhttp://www.artima.com/insidejvm/ed2/ch01IntroToJavasArchitect urePrint.html Chapter 5 The Java Virtual Machine http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1. html http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1. htmlhttp://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine1. html Interactive Illustrations http://www.artima.com/insidejvm/applets/index.html http://www.artima.com/insidejvm/applets/index.html
  • Slide 4
  • 4 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Java Programming Environment
  • Slide 5
  • 5 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Java Platform The byte code generated by the Java front-end is an intermediate formThe byte code generated by the Java front-end is an intermediate form Compact Platform-independent
  • Slide 6
  • 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation
  • Slide 7
  • 7 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Role of the Virtual Machime Local or Remote
  • Slide 8
  • 8 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Execution Engine Back-end transformation and executionBack-end transformation and execution Simple JVM: byte code interpretation Just-in-time compiler Method byte codes are compiled into machine code the first time they are invoked The machine code is cached for subsequent invocation It requires more memory Adaptive optimization The interpreter monitors the activity of the program, compiling the heavily used part of the program into machine code It is much faster than simple interpretation The memory requirement is only slightly larger due to the 20 % /80 % rule of program execution (In general, 20% of the code is responsible for 80% of the execution)
  • Slide 9
  • 9 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Java Virtual Machine
  • Slide 10
  • 10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Shared Data Areas
  • Slide 11
  • 11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Thread Data Areas Frame in Execution
  • Slide 12
  • 12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Stack Frames Stack frames have three parts:Stack frames have three parts: Local variables Operand stack Frame data
  • Slide 13
  • 13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Stack Frame Local Variables class Example3a { public static int runClassMethod(int i, long l, float f, double d, Object o, byte b) { public static int runClassMethod(int i, long l, float f, double d, Object o, byte b) { return 0; return 0; } public int runInstanceMethod(char c, double d, short s, boolean b) { public int runInstanceMethod(char c, double d, short s, boolean b) { return 0; return 0; }}
  • Slide 14
  • 14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Stack Frame Operand Stack Adding 2 numbers iload_0iload_1Iaddistore_2
  • Slide 15
  • 15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Execution Model Eternal Math ExampleEternal Math Example http://www.artima.com/insidejvm/applets/EternalMath.htm l http://www.artima.com/insidejvm/applets/EternalMath.htm lhttp://www.artima.com/insidejvm/applets/EternalMath.htm l
  • Slide 16
  • 16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Stack Frame Frame Data The stack frame also supportsThe stack frame also supports Constant pool resolution Normal method return Exception dispatch
  • Slide 17
  • 17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Stack Frame Frame Allocation in a Heap class Example3c { public static void addAndPrint() { public static void addAndPrint() { double result = addTwoTypes(1, 88.88); double result = addTwoTypes(1, 88.88); System.out.println(result) ; System.out.println(result) ; } public static double addTwoTypes(int i, double d) { public static double addTwoTypes(int i, double d) { return i + d; return i + d; }}
  • Slide 18
  • 18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Stack Frame Native Method A simulated stack of the target language (e.g. C) is created for JNIA simulated stack of the target language (e.g. C) is created for JNI
  • Slide 19
  • 19 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Heap Class instances and array are stores in a single, shared heapClass instances and array are stores in a single, shared heap Each Java application has its own heapEach Java application has its own heap Isolation But a JVM crash will break this isolation JVM heaps always implement garbage collection mechanismsJVM heaps always implement garbage collection mechanisms
  • Slide 20
  • 20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Heap Monolithic Object Representation
  • Slide 21
  • 21 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Heap Splitted Object Representation
  • Slide 22
  • 22 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example HeapOfFishHeapOfFish http://www.artima.com/insidejvm/applets/HeapOfFish.htm l http://www.artima.com/insidejvm/applets/HeapOfFish.htm lhttp://www.artima.com/insidejvm/applets/HeapOfFish.htm l
  • Slide 23
  • 23 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Heap Memory/Speed Tradeoff
  • Slide 24
  • 24 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Heap Arrays as Objects
  • Slide 25
  • 25 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Inside the Java 2 Virtual Machine by Bill VennersInside the Java 2 Virtual Machine by Bill Venners Ch 1 Ch 5 Illustrations

Recommended

View more >