An Introduction to Java Compiler and Runtime
-
Upload
omar-bashir -
Category
Software
-
view
275 -
download
2
Transcript of An Introduction to Java Compiler and Runtime
Java: Write Once Run AnywhereJava Source Code (.java files) Java Compiler Java Bytecode (.class files)
Java: Write Once Run AnywhereJava Source Code (.java files) Java Compiler Java Bytecode (.class files)
Java Virtual Machine
Java: Write Once Run AnywhereJava Source Code (.java files) Java Compiler Java Bytecode (.class files)
Java Virtual Machine
Interpretedplatform specificinstructions
Any Hardware
Java Virtual Machine (JVM)
● A virtual computer that executes Java bytecode.– A platform specific program giving Java its platform
independence.
● Key JVM concepts,– Specification
● Abstract definition omitting irrelevant implementation details.
– Implementation● Platform specific implementation that executes bytecode.
– Instance● Each Java application is executed in a separate JVM instance.● Launched by executing the java command.
Java HotSpotTM Virtual Machine
● Oracle's/Sun's JVM implementation.● Key features
– JIT (Just In Time) compilation● Compilation of specific program sections to native instructions
and caching them.
– Adaptive optimisation● Dynamic recompilation of program sections based on current
execution profiles.
● Generational garbage collection– Disposing unused objects and reclaiming memory.
JVM Architecture
Class Loader
Compiled Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
NativeLibraries
Thread common memory Thread specific memory
Code Cache
JVM Architecture
Thread common memory Thread specific memoryThread common memory
Dynamic on demand loading of Java classes from the classpath1. Loading bytecode,2. Linking,3. Variable initialisation
Class Loader
Compiled Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
NativeLibraries
Code Cache
JVM Architecture
Thread common memory Thread specific memory
Heap contains instances of classes, i.e., objects. Heap is garbage collected to remove unused objects.
Class Loader
Compiled Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
NativeLibraries
Code Cache
JVM Architecture
Thread common memory Thread specific memory
Method Area stores information regarding loaded classes, their fields, methods and values of class variables. Also referred to as Perm Gen, it no longer exists in Java 8.
Class Loader
Compiled Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
NativeLibraries
Code Cache
JVM Architecture
Thread common memory Thread specific memory
Code cache stores methods that have been compiled to native code.
Class Loader
Compiled Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
NativeLibraries
Code Cache
JVM Architecture
Thread common memory Thread specific memory
Stacks are per thread memory frames organised in LIFO (Last In First Out) structure.
Class Loader
Compiled Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
NativeLibraries
Code Cache
JVM Architecture
Thread common memory Thread specific memory
Program counter and three registers manage the stack. Stack-oriented design helps keep the JVM's instruction set and implementation small.
Program counter tracks instructions to be executed. optop register points to the top of operand stack, the workspace.
frame register points to operations of the stack.
vars register points to the local variables sections.
Class Loader
Compiled Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
NativeLibraries
Code Cache
javac – Java Compiler
● Part of JDK● Reads Java class and interface files and compiles
them into bytecode class files.– Source files have .java extensions.
– Bytecode files have .class extensions.
● Syntax– javac [options] [sources] [classes] [@argfiles]
● options: Command line options.● sources: Source files.● classes: Classes to be processed for annotations.● argfiles: Files listing options and source files.
javac – Java Compiler
● Most frequently used options,– -cp: Classpath, i.e., path to dependencies.
– -d: directory containing the compiled classes.
– -deprecation: Shows a description of each use of a deprecated member or class.
– -g: Generate debugging information.
javac – Java Compiler (Example)code
bin
src
app
calc
CalcApp.java
Calculator.java
javac src/*/*.java d bin
javac – Java Compiler (Example)code
bin
src
app
calc
CalcApp.java
Calculator.java
app
calc
CalcApp.class
Calculator.class
java – Launch Java Application in JVM
● Syntax– java [options] main-class [arguments …]
– java [options] -jar file.jar [arguments …]
● Most frequently used options,– -cp: Classpath, path to libraries and class files.
– -Dproperty=value: set a system property.
– -Xmsn: Minimum heap size.
– -Xmxn: Maximum heap size.
– -Xssn: Stack size.
jar – Java Archive
● Grouping class files into libraries and java executables.– Convenient packaging and deployment.
– Based on the zip file format.
– Command syntax closely related to tar
● Syntax– Creation,
● jar cf jarfile input-files● jar cmf manifest jarfile input-files
– Extraction● jar xf jarfile
● Manifests provide,– Versioning, signing, specifying classpaths, java executable.
Garbage Collection● JVM relieves coders from deleting unused objects.● Unused objects are deleted from the heap by the
Garbage Collector (GC).– i.e., objects whose reference is not assigned to any variable.
● Each application has two threads at least,– Thread running main,
– Thread running GC.
● When the GC thread runs, all other threads may pause (Dependant on GC implementation).– GC's execution is non-deterministic.
– Results in Java not being suitable for applications with strict timing constraints (i.e., real-time applications).
Garbage Collection
● Young generation– New objects created.
– Minor garbage collection removes dead objects.
– Surviving objects aged and moved to old generation.
● Old generation– Long surviving objects.
– Major garbage collection removes dead objects.
● Permanent generation– Metadata of methods and classes for the JVM
Eden S0 S1Eden Tenured Permanent
Young Generation
SurvivorSpace
Old Generation Permanent Generation
Garbage CollectionExample
import java.util.ArrayList;import java.util.List;
public class DemoGc {public static void main(String[] args) {
wait1Minute();for(int i = 0 ; i < 1000000; i++) {
List<Double> products = new ArrayList<Double>();for(int j = 0; j < 1000; j++) {
products.add(i * j * 1.0);}
System.out.print("***** " + i + "\r");}
}
private static void wait1Minute() { try { Thread.sleep(60000); } catch (Exception exp) {} }}