The Java Virtual Machine: The University of North Carolina at Chapel Hill
The Java Virtual Machine: The University of North Carolina at Chapel Hill
Reference
The 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/introarchP.html
Interactive Illustrations
http://www.artima.com/insidejvm/applets/index.html
Phases of Compilation
Local or Remote
Class Loaders
Bootstrap (default) loader (in the JVM) User-defined (custom) loaders
Custom loaders can download classes across a network (applets), get them out of some kind of database, or even calculate them on the fly.
10
Just-in-time compiler
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, a little more memory 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)
11
12
13
Frame in Execution
14
Stack Frames
15
Stack Frame
Local Variables
class Example3a { public static int runClassMethod(int i, long l, float f, double d, Object o, byte b) { return 0; } public int runInstanceMethod(char c, double d, short s, boolean b) { return 0; } }
16
Stack Frame
Operand Stack
Adding 2 numbers iload_0 iload_1 Iadd istore_2
Compiler can tell how many slots the op stack will need for a method
17
Stack Frame
Frame Data
The stack frame also supports
Constant pool resolution Normal method return Exception dispatch
18
Stack Frame
Frame Allocation in a Heap
class Example3c {
public static void addAndPrint() { double result = addTwoTypes(1, 88.88); System.out.println(result) ; } public static double addTwoTypes(int i, double d) { return i + d; }
}
19
Stack Frame
Native Method
A simulated stack of the target language (e.g. C) is created for JNI
20
The Heap
Class instances (objects) and arrays are stored in a single, shared heap Each Java application has its own heap
Isolation But a JVM crash will break this isolation
21
Heap
Monolithic Object Representation
22
23
The Heap
Memory/Speed Tradeoff
24
The Heap
Arrays as Objects
25
Examples
HeapOfFish
http://www.artima.com/insidejvm/applets/HeapOfFish.html Object allocation illustration
26
Reading Assignment
Inside the Java 2 Virtual Machine by Bill Venners
Ch 1 Ch 5 Illustrations
27