Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.

Similar presentations


Presentation on theme: "1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology."— Presentation transcript:

1 1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

2 2 2 Java Program class SumI { public static void main (String[] args) { int count=10; int sum =0; for (int index=1;index<count;index++) sum=sum+index; } // method main } // class SumI

3 3 3 Java ByteCode Method void main(java.lang.String[]) 0 bipush 10// byte push 10 to stack (0x10) 2 istore_1// load 10 (top of stack) to count 3 iconst_0// push 0 to stack 4 istore_2// load 0 (top of stack to sum 5 iconst_1// push 1 to stack 6 istore_3// load 1 (top of stack) to index 7 goto 17// go to 17

4 4 3 Java ByteCode 10 iload_2// load sum to stack 11 iload_3// load index to stack 12 iadd// add 13 istore_2// store “top of stack” to sum 14 iinc 3 1// index ++ 17 iload_3// load index to stack 18 iload_1// load count to stack 19 if_icmplt 10// if index < count goto 10 22 return

5 5 Internal Architecture of JVM Execution engine Class loader subsystem method area heap Java stacks pc registers native method stacks Runtime data area class files Native Method Interface Native Method Libraries

6 6 Internal Architecture of JVM Class loader subsystem: a mechanism for loading classes or interfaces. Execution engine: a mechanism for executing the instructions contained in the methods of loaded classes. Runtime data area: a memory to store bytecodes (method area), objects (heap), parameters, return values, local variables, (stack) results of intermediate computations (stack). JVM spec. is abstract ==> designers are free to implement JVM by their own structure.

7 7 JVM Execution JVM execution: A. class loading, B. Linking: Verification, Preparation, and Resolution C. Initialization D. Executing. Each instance of JVM has one method area and one heap Method area and heap are shared by all threads. JVM parses class and store methods info in method area. Objects are put onto heap.

8 8 Runtime Data Area Shared among threads class data class data class data class data class data class data class data class data class data class data class data class data Method area object heap

9 9 Threads Java supports multi-thread applications. Multi-thread: Processing can be broken into several separate threads of control which execute at the same time A thread is one sequential flow of execution that occurs at the same time another thread is executing the statement of the same program Each thread has its own PC register (program counter) and Java Stack. PC register: pointer to next instruction to be executed. Java Stack: parameters, return values, local variables, results of intermediate computations.

10 10 Thread’s Runtime Data Area thread 1 thread 2 thread 3 pc registers stack frame stack frame stack frame stack frame stack frame stack frame stack frame stack frame thread 1 stack frame stack frame stack frame stack frame stack frame stack frame thread 2 java stacks stack frame stack frame stack frame stack frame thread 3 native method stacks

11 11 Thread’s Runtime Data Area Java Stacks: state of Java method invocation. Native method stacks: state of native method invocation. Java Stack is composed of stack frames. Each stack frame corresponds to one method invocation. In the example: A. Thread 1 and 2 are executing Java methods: PC registers of Thread 1and 2 are pointed to the next instruction to be executed B. Thread 3 is executing a native method: PC register of Thread 3 is undefined.

12 12 Class Loader Subsystem Class loader subsystem: A. Loading: find and import the binary data for a type. B. Linking: 1. Verification: ensure the correctness of imported type. A. verify.class file is well-formed with a proper symbol table. B. verify that bytecode obeys the semantic requirements of the Java Virtual Machine. C. Example: VM checks 1. every instruction has a valid operation code 2. Every branch instruction branches to the start (not middle) of some other instruction.

13 13 Class Loader Subsystem B. Linking: 2. Preparation: A. allocating memory for class variables and initializing the memory to default values. B. allocating data structures that are used internally by the virtual machine: 1. method tables. 2. data structure that allows any method to be invoked on instances of a class without requiring a search of superclasses at invocation time.

14 14 Class Loader Subsystem B. Linking: 3. Resolution: A. transforming symbolic references (e.g. class.field) into direct references. B. symbolic reference is replaced with a direct reference that can be more efficiently processed if the reference is used repeatedly. (Quick instructions) C. Implementation choice: static linkage vs. laziest resolution

15 15 Class Loader Subsystem C. Initialization: invoke java code that initializes class variables to their proper staring values. A. execution of any class variable initializers B. Before a class can be initialized, its direct superclass must be initialized, as well as the direct superclass of its direct superclass, and so on, recursively. C. Initialization may cause loading, linking, and initialization errors D. Because Java is multithreaded, initialization of a class or interface requires careful synchronization.

16 16 Class Loader Subsystem JVM contains two kinds of class loader: A. Primordial class loader: load trusted class. It looks in the each directory, in the order the directories appear in the CLASSPATH, until a file with appropriate name (filename.class) is found. B. Class loader objects: 1. Class loader objects(java.lang.ClassLoader) are part of the Java APIs. 2. Three methods in ClassLoader (defineClass findSystemClass, resolveClass) are the gateway into JVM.

17 17 Class Loader Subsystem DefineClass converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using the newInstance method in class Class. findSystemClass

18 18 Method Area Inside a Java Virtual Machine Instance, information of about loaded types(classes and interface) are loaded into a logical area of memory called method area. A. Class loader reads in the class file (a linear stream of bytes) and pass it to VM. B. VM extracts information about the type from the binary data and stores the information in method area. PS: Class (static) variables are stored in method area. All threads share the Method area. (Thus, access to the data area’s data structure must be thread-safe.)

19 19 Type Information stored in Method Area Fully qualified name of the type. Fully qualified name of its superclass class or interface type’s modifier (public, abstract, final) List of interface Constant pool: literals, symbolic links to types, fields, methods. Field information: field name, type, modifiers Method information: name, return type, parameters, modifiers, method’s bytecodes, size of operand stack size of local variables, exception table

20 20 Type Information stored in Method Area Static variables: class variables are shared by all instances of a class. (must allocate space in method area before anyone uses it.) A reference to class ClassLoader: for dynamic linking A reference to class Class

21 21 Method Table The type information stored in method area must be organized to be quickly accessible. A method table of a class is an array of direct references to all its methods and the method inherited from its superclass.

22 22 Heap New objects are allocated by JVM in heap. A heap exists in every JVM instance. Thus, two different applications can not trample on each other’s heap. Heap are shared by all threads. Thus, synchronization between threads is needed. Garbage Collector is responsible for freeing Objects. Note objects (in heap) and class (in method area) may be freed by GC.

23 23 Object Representation JVM spec does not specify how object should be represented on the heap. The data structure of object representation and its GC may greatly influence performance. Three possible object representations: heap contains two parts: handle pool and object pool

24 24 ptr to object pool ptr to class data the handle pool the heap instance data the object pool class data the method area ptr to handle pool an object reference Splitting an object across a handle pool and object pool.

25 25 ptr to class data the heap instance data The method area class data ptr to heap an object reference Keeping object data all in one place.

26 26 prt to class data length=(2) ar[0] (ref) ar[1] (ref) the heap prt to class data length=(2) ar[0][0](int) ar[0][1] (int) prt to class data length=(2) ar[1][0] (int) ar[1][1] (int) class data for [[I class data for [I the method area ar (an array ref) int [ ] [ ] ar= new int [2][2]; One possible heap representation for arrays.

27 27 prt to full class data prt to method data ●●●●●● entry point into all data for the class method data method data method data prt to special structure instance data the heap ptr into heap method table the method area Keeping the method table close at hand.

28 28 Object Representation Each object (instance) of a class has a lock (mutex). Only one thread at a time can own the object’s lock. All threads that attempt to access to a locked object, are put into a wait set. (for wait(), notify() and notifyAll())

29 29 Program Counter Each thread of a running program has its own pc register (program counter). Program counter is created when the thread is started. Pc register can point to a native code or bytecode.

30 30 Java Stack When a new method is launched, JVM creates a new Java stack for the thread. A Java stack contains stack frames for method invocation. Java Stack frame: A. method’s local variables. B. operand stack. C. frame data (constant pool resolution, return values, return address, exception dispatch). A method can complete itself in either of two ways: normal and abrupt completion (exception). Either Way the stack frame is popped and discarded.

31 31 type long float double reference int typeindex 0 1 3 4 6 7 parameter int i long l float f double d Object o byte b reference int double int typeindex 0 1 2 4 5 parameter hidden this char c short s boolean b double d runClassMethod() runInstanceMethod() Class Example3a { // local variable in stack frame public static int runClassMethod(int i, long l, float f, double d, Object o, byte b) { return 0;} public int runInstanceMethod(int i, double d, short s, boolean b) { return 0;} }

32 32 100 98 0 1 2 local variables operand stack 100 98 100 0 1 2 98 100 98 0 1 2 100 98 198 0 1 2 100 98 198 0 1 2 before starting after iload_0 after iload_1 after iadd after istore_2 iload_0 // push local variable 0 (an int) iload_1 // push local variable 1 (an int) iadd // pop two ints, add them and push result istore_2 // pop int, store into local variable 2

33 33 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; } Java Stack Example

34 34 1 88.88 0 1 0 1 1 0 1 89.88 0 1 before invoke addTwoTypes() After invoke addTwoTypes() returns frames for addAndPrint( ) frame for addTwoTypes( ) local variables frame data operand stack

35 35 0 1 0 1 0 1 before invoke addTwoTypes() After invoke addTwoTypes() returns frames for addAndPrint( ) frame for addTwoTypes( ) 1 88.88 1 89.88

36 36 stack frame stack frame stack frame stack frame Java stacks a native method stack this Java method invokes a native method. the current frame This C function invokes another C function This C function invokes a Java method


Download ppt "1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology."

Similar presentations


Ads by Google