The Java VM Architecture & APIs



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 

Typical runtime system includes:   

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 

Typical runtime system includes(cont¶d): 

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 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 .

modifiers Bytecodes. type.Memory Management  The Method Area  Type Information    Fully qualified name of the type of itself. literals   Field Information  Name. modifiers Name.Java VM Architecture . superinterfaces Whether or not the type is a class or an interface Type¶s modifiers(public. final) Set of constants   Constant Pool(more detail later)  Symbolic references. return & arg. type. superclasses. stack frame size (not native or abstract methods) Method Information   . exception table. abstract.

Java VM Architecture .Memory Management  The Method Area  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)    A Reference to Class ClassLoader A Reference to Class Class  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   Method area also can be garbage collected as an unreferenced instance .

Allocation instruction exists. Method area and heap may be on the same heap. but freeing instruction doesn¶t exists!  Freeing memory occupied by objects that are no longer referenced is responsible for a garbage collector. . All threads share it!  So.  Every application has its own heap  But. careful synchronization of multi-threaded access to object is needed.Java VM Architecture .Memory Management  The Heap    The memory for the new object is allocated from a single heap.

easy for VM to combat heap fragmentation .need dereferencing two pointers (a) .Java VM Architecture .Memory Management  Object Representation in the heap   Objects can be freely represented in heap.Divides the heap into two parts . Two possible solution .

dereference pointer only once .make moving objects more complicated (b) .Java VM Architecture .Memory Management  Object Representation in the heap .

Memory Management  Method table    Can play an important role in achieving good VM performance. Method table includes :    Size of method¶s stack frame Method¶s bytecodes An exception table . May not exist in some implementation that have extremely low memory requirements.Java VM Architecture .

Java VM Architecture .Memory Management  Arrays in heap .

operand storage References. As each method is called. a stack frame is allocated. pc is undefined.  Local variables and operands are thread safe.Memory Management  The Program Counter    Each thread has its own PC. not actual objects can exist in stack.  The Stack     . Can be a native pointer or an offset from the beginning of method¶s bytecodes.Java VM Architecture . Each thread has its own stack area too. Used for local. If a thread is executing a native method.

Byte.Memory Management  Stack Frame Structure   Stack depth can be estimated at compiletime(will be discussed later) Locals(include arguments) :   Arguments Locals Frame data Operands Stack Frame Structure Instance method has hidden this reference on its first local slot. 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 :    . short.Java VM Architecture .

System.Java VM Architecture .println(result). } public static double addTwoTypes(int i. double d) { return i + d. } .Memory Management  Possible Implementations of the Java Stack  Example code : public static void addAndPrint() { double result = addTwoTypes(1.out.88). 88.

Java VM Architecture .Also saves time because Java VM doesn¶t need to copy the parameter values. .Memory Management  Possible Implementations of the Java Stack .Right one uses stack area more efficiently. .

Native method calling is just calling another method within itself. at the behest of the running Java program.Java VM Architecture . .Memory Management  Native Method Stack   A native method can access runtime data areas of VM and also do anything else.

Memory Management  Memory Hierachy .Java VM Architecture .

Java VM Architecture .Execution Relatives  Data Types .

Execution Relatives  Data Types    Each data types are defined according to the values they can have. but handled by byte array instructions   ReturnAddress  Not visible to programmer  Used internally with subroutine instructions(jsr.Java VM Architecture . ret) Array Object  Special object support by instruction set  All of array elements have the same type . Boolean type  Treated as integer  Boolean arrays are implemented as byte array  Made by newarray 4. Every data type except Double and Long needs one word(slot).

Stack is amenable to platform independence. Use ³escape´ or wide opcode to create an extended instruction set. byte.Java VM Architecture .  Hard to Extend   .  Increase instruction set encoding density . 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.Execution Relatives  Instruction Set  Advantages  Stack based ISA . some datatypes(short.No instruction fields are needed for specifying registers  Disadvantages  Non-Orthogonal Instruction Set  8-bit opcode can only encode 256 instructions.

Instruction Set  Instruction Set Format  Opcode byte + operand(more than zero)   Operand can be either of index. and char type. byte. Wide & escape code can be used to extend instruction set. immediate data or PC-relative offset.  Each of primitive types has its own instructions that can operate on them.Java VM Architecture .  Array access and type conversion instructions can only operate on short. .

iload_<n>  iinc . .code density.Instruction Set  Data-Movement Instructions     There can be different instructions for the same function. interpretation performance are related Pushing Constants onto the Stack  aconst_null. bipush(direct) Stack Manipulation Local Variable relatives  iload (index).Java VM Architecture . ldc(via constant pool). iconst_<n>.

arraylength Object relatives  new <index(to constant pool)>  (get|put)(static|field)  (checkcast|instanceof) <index(to constant pool)>   Type Conversion Functional Instructions    Only operates on int. Operands are converted to standard number representation before calculation Convert back to platform¶s internal representation and be pushed to stack after calculation. . <x>astore. anewarray.Instruction Set  Data-Movement Instructions   Array relatives  newarray. float. multianewarry. <x>aload.Java VM Architecture . double. long.

2> Return PC is saved on a stack(in frame data area). invoke(virtual|static|special|interface) <index1. but can not be accessed directly(only through return)  Method call     * Quick instructions Figure 5.11 .(no indirection) Also. compile-time PC offset.Java VM Architecture .Instruction Set  Control Flow Instructions  Designed to expose all control flow paths within a method    All control flow instructions use fixed. 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.

Java VM Architecture .  If an exception is not handled by the method that throws the exception.  unchecked(or runtime) exception caused by dynamic behavior of program All exceptions(and errors) must be handled somwhere. . stack frame is popped until the exception is handled by some handler.Exceptions and Errors  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.

  Use jsr. u2 handler_pc. athrow ± throw exception dereferenced by class name on top of the stack ex) From 8 To 12 Target 96 Type Arithmetic Exception  Use exception table to specify an exception handler.Java VM Architecture . u2 catch_type.Exceptions and Errors  Exception handler is implemented by miniature subroutines. } .   Internal data structure for exception table ExceptionTable { u2 from_pc. u2 to_pc. goto instruction. ret.

FileInputStream x = new java.out. } } } .println(³Not found´). } catch( and Errors  Exception handler example  Example Java code public class ExceptionTest { public static void main(String args[]) { try { } finally { System.FileNotFoundException e) { System.out.Java VM Architecture .println(³This must be executed´).FileInputStream(³myfile´).

int a(). [[J [Ljava/lang/Object [[[Z ()I ()Ljava/lang/String. D.Class File Structure   Magic Number  0xCAFEBABE(in big-endian order) constant . ([BII)V => => => => => => long[][] a. F. boolean[][][] a.Java VM Architecture . Z(boolean) ObjectType : L<classname> ArrayType : [(BaseType|ObjectType|ArrayType) Method : (<argument_types>)<return_types> ex) I => int a. S. J(long). I. java. int) Descriptors      .Object[] a. C. String a(). void a(byte[].lang. int.tag(u1) + length(u2) + bytes Tag specify type of constant  Constant Pool    CONST_??? BaseType : B.

and field . Super Classes. interface.Java VM Architecture .Class File Structure  Access Flags  Specify modifier of class.ACC_??? Specified by indexed constant in constant pool access_flags(u2) + name_index(u2) + descriptor_index(u2) + attribute_count(u2) + attributes_info name. Method   . Interface   Field. method. descriptor are on the constant pool  This.

max_locals. Source file name Code attribute ± max_stack.array dimensions.Class File Structure  Attribute    Method code. exception table length u1 . to a method  Class file limitation   .constant pool entries. bytecode length(per method). LineNumberTable. exception_table u2 . arg. constant value for finals.Java VM Architecture . code. method count. operand stack. field count. local variables. LocalVariableTable. exception that a method may throw Innerclass.

Java VM Architecture .Class File Structure ClassStruct_java.txt ClassStruct.txt .

let alone whether is behaves well or not.10   Halting Problem    Operand Stack Tracking   (b) ± stack size is different.Java VM Architecture . Studied by Alan Turing and Kurt Godel In general case.Class Verification  Class Verification   Class binaries are sometimes unsafe and may crash VM. Figure 5. it is not possible to take a description of a program and decide whether or not the program will complete. Must take all control path and prove that the program is safe in each case. For each alternative way in a method for reaching an instruction X. (c) ± operand types are different . the stack state and the local variable state must be equivalent.

Java VM Architecture . Execution engine doesn¶t need to perform runtime checks for following items :    Stack limits Types of arguments to JVM instructions Accesses or assignments to local variables .Class Verification  Operand Stack Tracking  ex) iconst_4 istore_1 Loop: aconst_null iinc 1 ±1 iload_1 Operand stack is not equivalent at Loop.  Stack tracking can be done in static-time   Because control flows are determined in static-time.

and the methods and fields of those Type conflict and access conflict Doesn¶t immediately check if the referenced class really exist.  . Speed up the initial loading time for a class  JVM verifier tries to delay the checks until they are necessary.Java VM Architecture .  The constant pool can also contain references to classes that haven¶t been loaded yet.Class Verification  Passing verification  Structure  Check that the class file structure is met. Type of constant and constant referencing instruction must be the same.  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.  Check also the contents of bytecode    Environment    Other classes that one class depends.

 The maximum stack length must not be exceeded.  Content  Each instruction should bee invoked with the correct types for its operands and stack values.Java VM Architecture .   Don¶t use the stack in complex ways. 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.  Use pop2. -> putfield myclass/myfield LAnotherClass.Class Verification  Passing verification  Environment  ex) invokenonvirtual myclass/funmethod()LFunClass   -> putfield myclass/myfield LFunClass.  .

stores stack and local var. verifier recursively examines. When a branch instruction is met.Class Verification  Working of bytecode verifier   Trace all the static control flow paths and simulate a stack symbolically. look at all the possible destinations   If a destination has not been seen previously. state in table  separate entry is maintained in the table for every instruction in bytecode    Then check that Instruction is begin run with the correct types Emulate the instruction¶s effect on the stack and local var. Else. Steps :  When instruction is first encountered. verifier compares the current state with recorded state .Java VM Architecture .

Java VM Architecture .Class Verification  Working of bytecode verifier  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 merge int float FilterInputStream int float Vector int float int incompatible .

txt  Java side method invocation from Native side   .Native Method Support  Java Native Interface(JNI)   Java side and native side can interoperate each other by JNI.txt Create JVM and call method through API ex) CreateJVM.Java VM Architecture .txt ->JNITest_header. 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.

servlet. etc. JMS. Light-weight platform for embedded system  J2EE(Enterprise Edition)    J2ME(Micro Edition)  .Java APIs . JSP. API for developing large enterprise software infrastructure ex) EJB.Java Platform Overview  J2SE(Standard Edition)  API for developing general user app. or client app.

Modifier. Constructor. . Field. it must implements the Serializable interface. Object.J2SE APIs  Serialization     RMI is used for communicating between objects in different VM.get(Fields|Methods| «) ex) Method can be called by invoke method of Method class.lang. In order to serialize an object.reflect package   Reflection     Array. Serialization may be used for object to be saved in persistent storage. Method. Determine class information at run time Classes in java. etc. Parameters or return values must be converted to implementation-independent form in RMI.getClass() -> Class.Java APIs .

Class Object declares five methods that enable programmers to access the Java Virtual Machine¶s support for the coordination aspect of synchronization.J2SE APIs  Thread   Multithreading support is provided by java. Thread execute run() method during its lifetime.Java APIs . notifyAll.lang. Suppported by instruction  monitorenter and monitorexit Locks are associated with each object and each class(through Class object).Thread class(and Runnable interface)  Libraries can communicate with the underlying OS.  notify. wait  Synchronization through monitor    .

Java APIs  Synchronization Example .