Академический Документы
Профессиональный Документы
Культура Документы
2003-12087
Contents
Java VM Architecture
Java Runtime Structure Memory Management Execution Relatives Exception and Error Management Class File Structure Class Verification Native Method Support(JNI) Java Platforms Overview Java APIs(J2SE)
Java APIs
Java VM Architecture
- Java Runtime Structure
Java VM
Usually referred to Java Runtime(JRE) Mainly used to execute programs written in Java
Execution Engine Virtual(or real hardware ex. ARM) processor for executing bytecodes Memory Manager Allocate memory for instances and arrays and perform garbage collection Error and Exception Manager Deal with exception
Java VM Architecture
- Java Runtime Structure
Native Method Support for calling c/c++ methods Threads Interface supporting threads and monitors Class Loader dynamically load Java classes from Java class files Security Manager verify that classes are safe and controlling access to system resources
Java VM Architecture
- Java Runtime structure
Java VM Architecture
- Memory Management
Memory Area
Divided into cells or slots Slot can usually hold a single data item All addressing is in terms of the logical memory cells.
Java VM Architecture
- Memory Management
Type Information
Fully qualified name of the type of itself, superclasses, superinterfaces Whether or not the type is a class or an interface Types modifiers(public, abstract, final) Set of constants
Field Information
Name, type, modifiers Name, return & arg. type, modifiers Bytecodes, exception table, stack frame size (not native or abstract methods)
Method Information
Java VM Architecture
- Memory Management
Class Variables
Class variables are shared among all instances Non-finals as part of data for the type that declares them Finals(constants) as part of data for the type that uses them(get a copy)
Class information can be accessed through class object Data structures that speed up access to the raw data ex) method table can have references to instance methods inherited from superclass
Method Table
Java VM Architecture
- Memory Management
The Heap
The memory for the new object is allocated from a single heap. Every application has its own heap But, All threads share it! So, careful synchronization of multi-threaded access to object is needed. Allocation instruction exists, but freeing instruction doesnt exists! Freeing memory occupied by objects that are no longer referenced is responsible for a garbage collector. Method area and heap may be on the same heap.
Java VM Architecture
- Memory Management
- Divides the heap into two parts - easy for VM to combat heap fragmentation - need dereferencing two pointers
(a)
Java VM Architecture
- Memory Management
(b)
Java VM Architecture
- Memory Management
Method table
Can play an important role in achieving good VM performance. May not exist in some implementation that have extremely low memory requirements. Method table includes :
Java VM Architecture
- Memory Management
Arrays in heap
Java VM Architecture
- Memory Management
Each thread has its own PC. Can be a native pointer or an offset from the beginning of methods bytecodes. If a thread is executing a native method, pc is undefined. Each thread has its own stack area too. Local variables and operands are thread safe. Used for local, operand storage References, not actual objects can exist in stack. As each method is called, a stack frame is allocated.
The Stack
Java VM Architecture
- Memory Management
Instance method has hidden this reference on its first local slot. Byte, short, char are converted into int (due to asymmetry of instruction set) Data to support constant pool resolution Exception table Normal method return address
Frame Data :
Java VM Architecture
- Memory Management
Example code :
public static void addAndPrint() { double result = addTwoTypes(1, 88.88); System.out.println(result); }
Java VM Architecture
- Memory Management
- Right one uses stack area more efficiently. - Also saves time because Java VM doesnt need to copy the parameter values.
Java VM Architecture
- Memory Management
A native method can access runtime data areas of VM and also do anything else. Native method calling is just calling another method within itself, at the behest of the running Java program.
Java VM Architecture
- Memory Management
Memory Hierachy
Java VM Architecture
- Execution Relatives
Data Types
Java VM Architecture
- Execution Relatives
Data Types
Each data types are defined according to the values they can have. Every data type except Double and Long needs one word(slot). Boolean type Treated as integer Boolean arrays are implemented as byte array
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
- No instruction fields are needed for specifying registers
8-bit opcode can only encode 256 instructions. some datatypes(short, byte, char) are relegated to second class status and receive less support in ISA Extending the machine to support 96-bit or 128-bit floats and longs cannot be done simply. Use escape or wide opcode to create an extended instruction set.
Hard to Extend
Java VM Architecture
- Instruction Set
Operand can be either of index, immediate data or PC-relative offset. Wide & escape code can be used to extend instruction set.
Each of primitive types has its own instructions that can operate on them.
Array access and type conversion instructions can only operate on short, byte, and char type.
Java VM Architecture
- Instruction Set
Data-Movement Instructions
There can be different instructions for the same function. - code density, interpretation performance are related Pushing Constants onto the Stack aconst_null, iconst_<n>, ldc(via constant pool), bipush(direct) Stack Manipulation Local Variable relatives iload (index), iload_<n> iinc
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)>
Only operates on int, float, double, long. Operands are converted to standard number representation before calculation Convert back to platforms internal representation and be pushed to stack after calculation.
Java VM Architecture
- Instruction Set
All control flow instructions use fixed, compile-time PC offset.(no indirection) Also, jump directly to a method via a fixed index into the constant pool This feature enables both complete code discovery and load-time stack tracking. invoke(virtual|static|special|interface) <index1,2> Return PC is saved on a stack(in frame data area), but can not be accessed directly(only through return)
Method call
Java VM Architecture
- Exceptions and Errors
Errors caused by either the application behavior and the limitation of VM Exception checked or unchecked checked exception must be encapsulated by try/catch clause. unchecked(or runtime) exception caused by dynamic behavior of program All exceptions(and errors) must be handled somwhere. If an exception is not handled by the method that throws the exception, stack frame is popped until the exception is handled by some handler.
Java VM Architecture
- Exceptions and Errors
Use jsr, ret, goto instruction. athrow throw exception dereferenced by class name on top of the stack ex) From 8 To 12 Target 96 Type
Arithmetic Exception
Java VM Architecture
- Exceptions and Errors
public class ExceptionTest { public static void main(String args[]) { 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
- Class File Structure
Magic Number
0xCAFEBABE(in big-endian order) constant - tag(u1) + length(u2) + bytes Tag specify type of constant
Constant Pool
CONST_??? BaseType : B, C, D, F, I, J(long), S, Z(boolean) ObjectType : L<classname> ArrayType : [(BaseType|ObjectType|ArrayType) Method : (<argument_types>)<return_types> ex) I => int a;
[[J [Ljava/lang/Object [[[Z ()I ()Ljava/lang/String; ([BII)V => => => => => => long[][] a; java.lang.Object[] a; boolean[][][] a; int a(); String a(); void a(byte[], int, int)
Descriptors
Java VM Architecture
- Class File Structure
Access Flags
Specify modifier of class, interface, method, and field - ACC_??? Specified by indexed constant in constant pool access_flags(u2) + name_index(u2) + descriptor_index(u2) + attribute_count(u2) + attributes_info name, descriptor are on the constant pool
Field, Method
Java VM Architecture
- Class File Structure
Attribute
Method code, constant value for finals, exception that a method may throw Innerclass, LineNumberTable, LocalVariableTable, Source file name Code attribute max_stack, max_locals, code, exception_table u2 - constant pool entries, field count, method count, bytecode length(per method), local variables, operand stack, exception table length u1 - array dimensions, arg. to a method
Java VM Architecture
- Class File Structure
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. 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. 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
Halting Problem
Java VM Architecture
- Class Verification
ex)
iconst_4
istore_1 Loop: aconst_null iinc 1 1 iload_1 Operand stack is not equivalent at Loop.
Because control flows are determined in static-time. Execution engine doesnt need to perform runtime checks for following items :
Stack limits Types of arguments to JVM instructions Accesses or assignments to local variables
Java VM Architecture
- Class Verification
Passing verification
Structure
ex) this_class field must be the index of a CONSTANT_Class Magic field must be the value 0xCAFEBABE Check that all byte code offsets are within method boundary. Type of constant and constant referencing instruction must be the same.
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. Speed up the initial loading time for a class
JVM verifier tries to delay the checks until they are necessary.
Java VM Architecture
- Class Verification
Passing verification
Content Each instruction should bee invoked with the correct types for its operands and stack values.
Use pop2, pop to retrieve long value from the stack Stack size is specified in the Code attribute 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
Then check that Instruction is begin run with the correct types Emulate the instructions effect on the stack and local var. When a branch instruction is met, look at all the possible destinations
If a destination has not been seen previously, verifier recursively examines. Else, verifier compares the current state with recorded state
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
merge
incompatible
Java VM Architecture
- Native Method Support
Java side and native side can interoperate each other by JNI. Native side method invocation from Java side
Use native keywords for modifier of function Generate header file for native function by javah ex) JNITest_java.txt ->JNITest_header.txt Create JVM and call method through API ex) CreateJVM.txt
Java APIs
- Java Platform Overview
J2SE(Standard Edition)
API for developing general user app. or client app. API for developing large enterprise software infrastructure ex) EJB, servlet, JSP, JMS, etc. Light-weight platform for embedded system
J2EE(Enterprise Edition)
J2ME(Micro 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. Determine class information at run time Classes in java.lang.reflect package
Reflection
Object.getClass() -> Class.get(Fields|Methods| ) ex) Method can be called by invoke method of Method class.
Java APIs
- J2SE APIs
Thread
Multithreading support is provided by java.lang.Thread class(and Runnable interface) Libraries can communicate with the underlying OS. Thread execute run() method during its lifetime. 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