Java Jvm Detail
-
Upload
jigar-shah -
Category
Documents
-
view
238 -
download
2
description
Transcript of Java Jvm Detail
-
The Java VM Architecture & APIs
2003-12087
-
Contents
Java VM ArchitectureJava Runtime StructureMemory ManagementExecution RelativesException and Error ManagementClass File StructureClass VerificationNative Method Support(JNI)Java APIsJava Platforms OverviewJava APIs(J2SE) -
Java VM Architecture
Java VMUsually referred to Java Runtime(JRE)Mainly used to execute programs written in Java
- Java Runtime Structure
Typical runtime system includes:Execution Engine Virtual(or real hardware ex. ARM) processor for executing bytecodesMemory Manager Allocate memory for instances and arrays and perform garbage collectionError and Exception Manager Deal with exception -
Java VM Architecture
Typical runtime system includes(contd):Native Method Support for calling c/c++ methodsThreads Interface supporting threads and monitorsClass Loader dynamically load Java classes from Java class filesSecurity Manager verify that classes are safe and controlling access to system resources
- Java Runtime Structure -
Java VM Architecture
- Java Runtime structure -
Java VM Architecture
Memory AreaDivided into cells or slotsSlot can usually hold a single data itemAll addressing is in terms of the logical memory cells.
- Memory Management -
Java VM Architecture
The Method AreaType InformationFully qualified name of the type of itself, superclasses, superinterfacesWhether or not the type is a class or an interfaceTypes modifiers(public, abstract, final)Constant Pool(more detail later)Set of constantsSymbolic references, literalsField InformationName, type, modifiersMethod InformationName, return & arg. type, modifiersBytecodes, exception table, stack frame size
- Memory Management(not native or abstract methods)
-
Java VM Architecture
The Method AreaClass VariablesClass variables are shared among all instancesNon-finals as part of data for the type that declares themFinals(constants) as part of data for the type that uses them(get a copy)A Reference to Class ClassLoaderA Reference to Class ClassClass information can be accessed through class objectMethod TableData structures that speed up access to the raw data
- Memory Managementex) method table can have references to instance methods inherited from superclass
Method area also can be garbage collected as an unreferenced instance -
Java VM Architecture
The HeapThe memory for the new object is allocated from a single heap.Every application has its own heapBut, 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.
- Memory Management -
Java VM Architecture
Object Representation in the heapObjects can be freely represented in heap.Two possible solution
- Memory Management(a)
Divides the heap into two parts easy for VM to combat heapfragmentation
need dereferencing two pointers -
Java VM Architecture
Object Representation in the heap dereference pointer only once make moving objects more complicated
- Memory Management(b)
-
Java VM Architecture
Method tableCan play an important role in achieving good VM performance.May not exist in some implementation that have extremely low memory requirements.Method table includes :Size of methods stack frameMethods bytecodesAn exception table
- Memory Management -
Java VM Architecture
Arrays in heap
- Memory Management -
Java VM Architecture
The Program CounterEach 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.The StackEach thread has its own stack area too.Local variables and operands are thread safe.Used for local, operand storageReferences, not actual objects can exist in stack.As each method is called, a stack frame is allocated.
- Memory Management -
Java VM Architecture
Stack Frame StructureStack depth can be estimated at compile-time(will be discussed later)Locals(include arguments) :Instance method has hidden this reference on its first local slot.Byte, short, char are converted into int
- Memory Management(due to asymmetry of instruction set)
Frame Data :Data to support constant pool resolutionException tableNormal method return addressStack Frame
Structure
Arguments
Locals
Frame data
Operands
-
Java VM Architecture
Possible Implementations of the Java StackExample code :
- Memory Managementpublic 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
Possible Implementations of the Java Stack Right one uses stack area more efficiently. Also saves time because Java VM doesnt need to copy the parameter values.
- Memory Management -
Java VM Architecture
Native Method StackA 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.
- Memory Management -
Java VM Architecture
Memory Hierachy
- Memory Management -
Java VM Architecture
Data Types
- Execution Relatives -
Java VM Architecture
Data TypesEach data types are defined according to the values they can have.Every data type except Double and Long needs one word(slot).Boolean typeTreated as integerBoolean arrays are implemented as byte arrayMade by newarray 4, but handled by byte array instructionsReturnAddressNot visible to programmerUsed internally with subroutine instructions(jsr, ret)Array ObjectSpecial object support by instruction setAll of array elements have the same type
- Execution Relatives -
Java VM Architecture
Instruction SetAdvantagesStack based ISA - Stack is amenable to platform independence.Increase instruction set encoding density
- Execution Relatives- No instruction fields are needed for specifying registers
DisadvantagesNon-Orthogonal Instruction Set8-bit opcode can only encode 256 instructions.some datatypes(short, byte, char) are relegated to second class status and receive less support in ISA
Hard to ExtendExtending 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. -
Java VM Architecture
Instruction Set FormatOpcode byte + operand(more than zero)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.
- Instruction Set -
Java VM Architecture
Data-Movement InstructionsThere can be different instructions for the same function.
- Instruction Set- code density, interpretation performance are related
Pushing Constants onto the Stackaconst_null, iconst_, ldc(via constant pool), bipush(direct)Stack ManipulationLocal Variable relativesiload (index), iload_iinc -
Java VM Architecture
Data-Movement InstructionsArray relativesnewarray, anewarray, multianewarry, aload, astore, arraylengthObject relativesnew (get|put)(static|field)(checkcast|instanceof) Type ConversionFunctional InstructionsOnly operates on int, float, double, long.Operands are converted to standard number representation before calculationConvert back to platforms internal representation and be pushed to stack after calculation.
- Instruction Set -
Java VM Architecture
Control Flow InstructionsDesigned to expose all control flow paths within a methodAll control flow instructions use fixed, compile-time PC offset.(no indirection)Also, jump directly to a method via a fixed index into the constant poolThis feature enables both complete code discovery and load-time stack tracking.Method callinvoke(virtual|static|special|interface) Return PC is saved on a stack(in frame data area), but can not be accessed directly(only through return)* Quick instructionsFigure 5.11
- Instruction Set -
Java VM Architecture
Exceptions and ErrorsErrors caused by either the application behavior and the limitation of VMException checked or uncheckedchecked exception must be encapsulated by try/catch clause.unchecked(or runtime) exception caused by dynamic behavior of programAll 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.
- Exceptions and Errors -
Java VM Architecture
Exception handler is implemented by miniature subroutines.Use jsr, ret, goto instruction.athrow throw exception dereferenced by class name on top of the stackUse exception table to specify an exception handler.ex)Internal data structure for exception table
- Exceptions and ErrorsExceptionTable {
u2from_pc;
u2to_pc;
u2handler_pc;
u2catch_type;
}
FromToTargetType81296Arithmetic Exception -
Java VM Architecture
Exception handler exampleExample Java code
- Exceptions and Errorspublic 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
Magic Number0xCAFEBABE(in big-endian order)Constant Poolconstant - tag(u1) + length(u2) + bytesTag specify type of constantCONST_???DescriptorsBaseType : B, C, D, F, I, J(long), S, Z(boolean)ObjectType : LArrayType : [(BaseType|ObjectType|ArrayType)Method : ()ex) I=> int a;
- Class File Structure[[J=>long[][] a;
[Ljava/lang/Object=>java.lang.Object[] a;
[[[Z=>boolean[][][] a;
()I=>int a();
()Ljava/lang/String;=>String a();
([BII)V=>void a(byte[], int, int)
-
Java VM Architecture
Access FlagsSpecify modifier of class, interface, method, and field - ACC_???This, Super Classes, InterfaceSpecified by indexed constant in constant poolField, Methodaccess_flags(u2) + name_index(u2) + descriptor_index(u2) + attribute_count(u2) + attributes_infoname, descriptor are on the constant pool
- Class File Structure -
Java VM Architecture
AttributeMethod code, constant value for finals, exception that a method may throwInnerclass, LineNumberTable, LocalVariableTable, Source file nameCode attribute max_stack, max_locals, code, exception_tableClass file limitationu2 - constant pool entries, field count, method count, bytecode length(per method), local variables, operand stack, exception table lengthu1 - array dimensions, arg. to a method
- Class File Structure -
Java VM Architecture
- Class File StructureClassStruct.txt
ClassStruct_java.txt
-
Java VM Architecture
Class VerificationClass binaries are sometimes unsafe and may crash VM.Must take all control path and prove that the program is safe in each case.Halting ProblemStudied by Alan Turing and Kurt GodelIn 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 TrackingFor 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(b) stack size is different. (c) operand types are different
- Class Verification -
Java VM Architecture
Operand Stack Trackingex) iconst_4
- Class Verificationistore_1
Loop:
aconst_null
iinc 1 1
iload_1
Operand stack is not equivalent at Loop.
Stack tracking can be done in static-timeBecause control flows are determined in static-time.Execution engine doesnt need to perform runtime checks for following items :Stack limitsTypes of arguments to JVM instructionsAccesses or assignments to local variables -
Java VM Architecture
Passing verificationStructureCheck that the class file structure is met.ex) this_class field must be the index of a CONSTANT_Class Magic field must be the value 0xCAFEBABECheck also the contents of bytecodeCheck that all byte code offsets are within method boundary.Type of constant and constant referencing instruction must be the same.EnvironmentOther classes that one class depends, and the methods and fields of thoseType conflict and access conflictDoesnt 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.Speed up the initial loading time for a class
- Class Verification -
Java VM Architecture
Passing verificationEnvironmentex) invokenonvirtual myclass/funmethod()LFunClass-> putfield myclass/myfield LFunClass;-> putfield myclass/myfield LAnotherClass;ContentEach instruction should bee invoked with the correct types for its operands and stack values.Use pop2, pop to retrieve long value from the stackThe maximum stack length must not be exceeded.Stack size is specified in the Code attributeDont use the stack in complex ways.Only push items onto the stack just before they are needed.
- Class Verification -
Java VM Architecture
Working of bytecode verifierTrace all the static control flow paths and simulate a stack symbolically.Steps :When instruction is first encountered, stores stack and local var. state in tableseparate entry is maintained in the table for every instruction in bytecodeThen check that Instruction is begin run with the correct typesEmulate the instructions effect on the stack and local var.When a branch instruction is met, look at all the possible destinationsIf a destination has not been seen previously, verifier recursively examines.Else, verifier compares the current state with recorded state
- Class Verification -
Java VM Architecture
Working of bytecode verifierState comparisonTwo states are identical move forwardTwo states are incompatible verifier complains!Two state are compatible merges two states
- Class Verificationmerge
incompatible
intfloatDataInputStreamintfloatBufferedInputStreamintfloatVectorintfloatintintfloatFilterInputStream -
Java VM Architecture
Java Native Interface(JNI)Java side and native side can interoperate each other by JNI.Native side method invocation from Java sideUse native keywords for modifier of functionGenerate header file for native function by javahex) JNITest_java.txt ->JNITest_header.txtJava side method invocation from Native sideCreate JVM and call method through APIex) CreateJVM.txt
- Native Method Support -
Java APIs
J2SE(Standard Edition)API for developing general user app. or client app. J2EE(Enterprise Edition)API for developing large enterprise software infrastructureex) EJB, servlet, JSP, JMS, etc.J2ME(Micro Edition)Light-weight platform for embedded system
- Java Platform Overview -
Java APIs
SerializationRMI 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.ReflectionDetermine class information at run timeClasses in java.lang.reflect packageArray, Constructor, Field, Method, Modifier, etc.Object.getClass() -> Class.get(Fields|Methods| )ex) Method can be called by invoke method of Method class.
- J2SE APIs -
Java APIs
ThreadMultithreading 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.Synchronization through monitorSuppported by instructionmonitorenter and monitorexitLocks 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
- J2SE APIs -
Java APIs
Synchronization Example