1 / 32

ICS201 Lecture 10 : Introduction to Java Virtual Machine

King Fahd University of Petroleum & Minerals College of Computer Science & Engineering. Information & Computer Science Department. ICS201 Lecture 10 : Introduction to Java Virtual Machine. Outline. Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine

lharpole
Download Presentation

ICS201 Lecture 10 : Introduction to Java Virtual Machine

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department ICS201 Lecture 10 : Introduction to Java Virtual Machine

  2. Outline • Java Language, Java Virtual Machine and Java Platform • Organization of Java Virtual Machine • Garbage Collection • Interpreter and Just-In-Time Compiler

  3. Why Java ? Currently, Java is the most popular language in the world !

  4. Classical compilation model gccfor Win WindowsX86 Hello Hello.c gccfor Mac MacPowerPC Hello gccfor Linux LinuxX86 Hello Hello

  5. Java Compilation Model WindowsX86 Hello Hello.java MacPowerPC javac Hello.class Hello LinuxX86 Hello Hello

  6. The Big Picture Java Language Specification A.java B.java C.java Java Compiler A.class B.class C.class Java Virtual Machine Specification Java Virtual Machine

  7. Java Platforms (1) • A Java Platform consists • Java Virtual Machine  CPU • A set of standard classes  API • JVM in all platforms must satisfy JVM spec. • Standard classes can be tailored according to the resource constraints • Three levels of Java platforms:J2EE, J2SE and J2ME

  8. Java Platforms (2) From KVM White Paper (Sun Microsystem)

  9. What Is in the JVM Spec? • Bytecodes– the instruction set for Java Virtual Machine • Class File Format– The platform independent representation of Java binary code • Verification Rules– the algorithm for identifying programs that cannot compromise the integrity of the JVM

  10. Class Area Heap Stack Native Stack Class Information Constant Pool Method Area Internet *.class Execution Engine PC, FP, SP Registers Native Interface Class Loader File System *.class Native Methods Organization of JVM

  11. Class Loader • Loading: finding and importing the binary data for a class • Linking: • Verification: ensuring the correctness of the imported type • Preparation: allocating memory for class variables and initializing the memory to default values • Resolution: transforming symbolic references from the type into direct references. • Initialization: invoking Java code that initializes class variables to their proper starting values

  12. Class Area • Class Information • Internal representation of Java classes • Information about the superclass and implemented interfaces • Information about the fields and methods • Constant Pool • Method Area • Contains the bytecodes of the methods of the loaded classes

  13. Class Area Heap Stack Native Stack Class Information Constant Pool Method Area Internet *.class Execution Engine PC, FP, SP Registers Native Interface Class Loader File System *.class Native Methods Organization of JVM

  14. Stack • The Java stack is composed of frames • A frame contains the state of one Java method invocation • Logically, a frame has two parts: local variable array and operand stack • JVM has no registers; it uses the operand stack for storage of intermediate data values • to keep the JVM's instruction set compact • to facilitate implementation on architectures with limited number of general purpose registers • Each Java thread has its own stack and cannot access the stack of other threads

  15. Stack Frame Inter- Mediate Data Values public class A { ... ... void f(int x) { int i; for(i=0; i<x; i++) { ... ... } ... ... } Operand Stack SP i Local Variable Array x Caller’s SP Caller’s FP Return PC FP

  16. Bytecode iload_2 (load variable b) iload_3 (load variable c) iadd (addition) iconst_2 (load constant value 2) imul (multiplication) istore_1 (save the value of a) Example a = (b + c) 2

  17. Stack – Each Thread Has its own Stack Heap Thread 1 Thread 2 Thread 3 Stack Frame Frame Frame Frame Frame Frame Frame Frame

  18. Class Area Heap Stack Native Stack Class Information Constant Pool Method Area Internet *.class Execution Engine PC, FP, SP Registers Native Interface Class Loader File System *.class Native Methods Organization of JVM

  19. Heap • All Java objects are allocated in the heap • Java applications cannot explicitly free an object • The Garbage Collector is invoked from time to time automatically to reclaim the objects that are no longer needed by the application • The heap is shared by all Java threads

  20. Java Objects in the Heap Heap clazz Fields 1 Fields 2 Class Area … … class Object Fields n class A clazz class B … … clazz … …

  21. E E F F G G Garbage Collector • Roots: internally defined by the JVM implementation Live Objects: reachable from the roots Garbage (Dead Objects): not reachable from the roots, not accessible to the application root root B B A A D D C C

  22. root root root root A A B B B B E C C G D D E E E F F G G G Free Live Garbage Unknown Mark / Sweep / Compaction Before GC After Mark After Sweep After Compact

  23. Class Area Heap Stack Native Stack Class Information Constant Pool Method Area Internet *.class Execution Engine PC, FP, SP Registers Native Interface Class Loader File System *.class Native Methods Organization of JVM

  24. Execution Engine • Executes Java bytecodes either using interpreter or Just-In-Time compiler • Registers: • PC: Program Counter • FP: Frame Pointer • SP: Operand Stack Top Pointer

  25. Interpreter vs Just-In-Time Compiler Bytecode Bytecode Native Code JIT Compiler Interpreter CPU CPU Interpretation JIT Compilation

  26. Bytecode iload_2 (load variable b) iload_3 (load variable c) iadd (addition) iconst_2 (load constant value 2) imul (multiplication) istore_1 (save the value of a) Interpretation a = (b + c) 2

  27. Bytecode Interpreter (1) while(program not end ) { fetch next bytecode => b switch(b) { case ILOAD: load an integer from the local variable array and push on top of current operand stack; case ISTORE: pop an integer from the top of current operand stack and store it into the local variable array; case ALOAD: ... ... } // end of switch } // end of while

  28. Bytecode interpreter (2) • Advantage • Ease to implement • Does not need extra memory to store compiled code • Disadvantage • Very Slow: 10 ~ 100 times slower than execution of native code

  29. Just-In-Time Compiler • Dynamically compiles bytecode into native code at runtime, usually in method granularity • Execution of native code is much faster than interpretation of bytecode • Compilation is time consuming and may slow down the application • Tradeoffs between execution time and compilation time

  30. Adaptive Compiler • Observation: less than 20% of the methods account for more than 80% of execution time • Methods contains loop with large number of iteration; • Methods that are frequently invoked • Idea 1: only compile the methods where the application spends a lot of time • Idea 2: perform advanced compiler optimization for the hottest methods, simple or no compiler optimization for less hot methods

  31. How Adaptive Compiler Works • Set three thresholds T1, T2 (T1<T2) • Each method has a counter that is initialized to 0. Whenever the method is invoked, increase its counter by 1 • The methods with counter lower than T1 are executed using interpreter • When a method’s counter reaches T1, compile this method with simple optimizations • When a method’s counter reaches T2, recompile this method with advanced optimizations

  32. The end

More Related