The Java VM Architecture & Apis
The Java VM Architecture & Apis
The Java VM Architecture & Apis
2003-12087
Contents
Java VM Architecture
Java APIs
Java VM Architecture
Java VM
Java VM Architecture
Java VM Architecture
Java VM Architecture
- Memory Management
Memory Area
Java VM Architecture
- Memory Management
Set of constants
Field Information
Method Information
Java VM Architecture
- Memory Management
Method Table
Java VM Architecture
- Memory Management
The Heap
Java VM Architecture
- Memory Management
(a)
Java VM Architecture
- Memory Management
Object
(b)
Java VM Architecture
- Memory Management
Method
table
Java VM Architecture
- Memory Management
Arrays
in heap
Java VM Architecture
- Memory Management
The Stack
Java VM Architecture
- Memory Management
Stack
Frame Structure
Frame Data :
Arguments
Locals
Frame data
Operands
Stack Frame
Structure
Java VM Architecture
- Memory Management
Possible
Example code :
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;
}
Java VM Architecture
- Memory Management
Possible
Java VM Architecture
- Memory Management
Java VM Architecture
- Memory Management
Memory
Hierachy
Java VM Architecture
- Execution Relatives
Data
Types
Java VM Architecture
- Execution Relatives
Data Types
ReturnAddress
Not visible to programmer
Used internally with subroutine instructions(jsr, ret)
Array Object
Special object support by instruction set
All of array elements have the same type
Java VM Architecture
- Execution Relatives
Instruction Set
Advantages
Stack based ISA - Stack is amenable to platform
independence.
Increase instruction set encoding density
Disadvantages
Non-Orthogonal Instruction Set
Hard to Extend
Java VM Architecture
- Instruction Set
Instruction
Set Format
Java VM Architecture
- Instruction Set
Data-Movement Instructions
Java VM Architecture
- Instruction Set
Data-Movement Instructions
Array relatives
newarray, anewarray, multianewarry, <x>aload, <x>astore,
arraylength
Object relatives
new <index(to constant pool)>
(get|put)(static|field)
(checkcast|instanceof) <index(to constant pool)>
Type Conversion
Functional Instructions
Java VM Architecture
- Instruction Set
Method call
* Quick instructions
Figure 5.11
Java VM Architecture
- Exceptions and Errors
Java VM Architecture
- Exceptions and Errors
ex)
From
To
Target
Type
12
96
Arithmetic
Exception
Java VM Architecture
- Exceptions and Errors
try {
java.io.FileInputStream x
= new java.io.FileInputStream(myfile);
} catch(java.io.FileNotFoundException e) {
System.out.println(Not found);
} finally {
System.out.println(This must be executed);
}
Java VM Architecture
Magic Number
Constant Pool
CONST_???
Descriptors
=>
=>
=>
long[][] a;
java.lang.Object[] a;
boolean[][][] a;
()I
()Ljava/lang/String;
([BII)V
=>
=>
=>
int a();
String a();
void a(byte[], int, int)
Java VM Architecture
Access Flags
Field, Method
access_flags(u2) + name_index(u2) +
descriptor_index(u2) + attribute_count(u2) +
attributes_info
name, descriptor are on the constant pool
Java VM Architecture
Attribute
Java VM Architecture
ClassStruct_java.txt
ClassStruct.txt
Java VM Architecture
- Class Verification
Class Verification
Class binaries are sometimes unsafe and may crash VM.
Must take all control path and prove that the program is safe in
each case.
Halting Problem
Studied by Alan Turing and Kurt Godel
In general case, it is not possible to take a description of a
program and decide whether or not the program will complete, let
alone whether is behaves well or not.
Operand Stack Tracking
For each alternative way in a method for reaching an instruction X,
the stack state and the local variable state must be equivalent.
Figure 5.10
Java VM Architecture
- Class Verification
Stack limits
Types of arguments to JVM instructions
Accesses or assignments to local variables
Java VM Architecture
- Class Verification
Passing verification
Structure
Environment
Other classes that one class depends, and the methods and fields of
those
Type conflict and access conflict
Doesnt immediately check if the referenced class really exist.
The constant pool can also contain references to classes that havent been
loaded yet.
JVM verifier tries to delay the checks until they are necessary.
Java VM Architecture
- Class Verification
Passing verification
Environment
ex) invokenonvirtual myclass/funmethod()LFunClass
Content
Each instruction should bee invoked with the correct types
for its operands and stack values.
Only push items onto the stack just before they are needed.
Java VM Architecture
- Class Verification
Trace all the static control flow paths and simulate a stack
symbolically.
Steps :
When instruction is first encountered, stores stack and local
var. state in table
Java VM Architecture
- Class Verification
State comparison
Two states are identical move forward
Two states are incompatible verifier complains!
Two state are compatible merges two states
int
float
DataInputStream
int
float
BufferedInputStream
int
float
Vector
int
float
int
merge
incompatible
int
float
FilterInputStream
Java VM Architecture
Java
Native Interface(JNI)
Java APIs
Edition)
J2EE(Enterprise
J2ME(Micro
Edition)
Edition)
Java APIs
- J2SE APIs
Serialization
RMI is used for communicating between objects in different VM.
Parameters or return values must be converted to
implementation-independent form in RMI.
Serialization may be used for object to be saved in persistent
storage.
In order to serialize an object, it must implements the Serializable
interface.
Reflection
Determine class information at run time
Classes in java.lang.reflect package
Java APIs
- J2SE APIs
Thread
Suppported by instruction
monitorenter and monitorexit
Locks are associated with each object and each
class(through Class object).
Class Object declares five methods that enable
programmers to access the Java Virtual Machines support
for the coordination aspect of synchronization.
notify, notifyAll, wait
Java APIs
Synchronization Example