3. Java: Write Once Run Anywhere
Java Source Code (.java files) Java Compiler
4. Java: Write Once Run Anywhere
Java Source Code (.java files) Java Compiler Java Bytecode (.class files)
5. Java: Write Once Run Anywhere
Java Source Code (.java files) Java Compiler Java Bytecode (.class files)
Java Virtual Machine
6. Java: Write Once Run Anywhere
Java Source Code (.java files) Java Compiler Java Bytecode (.class files)
Java Virtual Machine
Interpreted
platform specific
instructions
Any Hardware
7. 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.
8. 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.
9. JVM Architecture
Class Loader
Compiled
Classes
JVM Runtime Memory
Heap
Method Area
Java Stack
Native Method Stack
Program Counter Registers
Execution Engine Native Interface
Native
Libraries
Thread common memory Thread specific memory
Code Cache
10. JVM Architecture
Thread common memory Thread specific memoryThread common memory
Dynamic on demand loading of Java
classes from the classpath
1. 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
Native
Libraries
Code Cache
11. 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
Native
Libraries
Code Cache
12. 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
Native
Libraries
Code Cache
13. 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
Native
Libraries
Code Cache
14. 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
Native
Libraries
Code Cache
15. 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
Native
Libraries
Code Cache
18. 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.
19. 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.
20. javac – Java Compiler (Example)
code
bin
src
app
calc
CalcApp.java
Calculator.java
javac src/*/*.java d bin
24. 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.
26. 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).
28. 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
Survivor
Space
Old Generation Permanent Generation