Download JVM2 - Webcourse

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JVM
in Context
oop
What’s a JVM
 Platform-independent execution environment
 A different JVM is required for each operating
system
 Any JVM can run the same version of a Java
program
 Converts Java bytecode into machine
language and executes it.
oop
Platform independence
 Programs can run without change on any
networked computer
 Simple administration
 Programs can run on embedded devices
 Printers, scanners…
 Networks as homogeneous execution
environments
 Enables building distributed systems
 Reduce time and cost of development
 More potential customers
oop
JVM in Java Architecture
 Java’s architecture main technologies:
 The Java programming language
Source files
 The Java class file format
Compiled files
 The Java API
Provide access to system resources
 The Java virtual machine
Runs the class files
oop
The Java Programming Environment
Compile time environment
A.Java
B.Java
C.Java
oop
B.class
A.class
B.class
C.class
Java
Virtual
Machine
Java
Compiler
A.class
run time environment
C.class
Object class
String class
Same Program, Different Platforms
oop
Java
Program
Java
Program
Java
Program
Java
Program
Java Platform
for
Linux
Java Platform
for
Win32
Java Platform
for your
Television
Java Platform
for your
Toaster
Linux Box
PC running
Windows XP
Your
Television
Your
Toaster
The JVM Roles
 Load class files
 Both program and API files
 Only files from API that are actually needed by
the program are loaded
 Execute bytecode
 Interpretation – one method bytecode at a time
 Just in time compiler – compiles bytecodes of
a method to native machine code the first time
the method is invoked, cache the native code.
 Adaptive optimizer – identifies the most heavily
used areas, compile them to native code.
oop
Basic JVM Components
The Java Virtual Machine
Program
Class
files
The Java
API’s
class files
Class
loader
Execution
engine
Native methods invocation
Host operating system
oop
The Java Class File
 The binary form for Java programs
 Represents a complete description of one Java
class or interface
 Platform independent – bytecodes are the machine
language of the JVM
 Not necessarily linked to the java language:
Java
Program
in Java
Lang.
Program
in other
Lang.
Compiler
class
files
Java
Compiler
class
files
Program
in Java
Lang.
oop
Compiler
Other
Binary
format
The Java API
 Set of runtime libraries
 Provide access to system resources of the host
computer
 A required component of any implementation of
the Java platform
Java
Program
Java Methods (Java API)
Native methods (dynamic libraries)
Host operating system
oop
JVM Major Subsystems and Memory
Areas.
oop
The Class Loader Subsystem



oop
The class loader performs three main functions of JVM,
namely: loading, linking and initialization.
The linking process consists of three sub-tasks, namely,
verification, preparation, and resolution, as shown by the
following figure.
These activities are performed in a strict order as shown by the
figure.
Class Loading
 Loading means reading the class file for a type, parsing
it to get its information, and storing the information in
the method area.
 For each type, the JVM stores reflection information
and bytecodes in the method area.
oop
Linking : Verification
 Ensuring that binary representation of a class is
structurally correct.
 Checks that each instruction will find a correctly
formed stack and local variable array
 checks that the items on top of the stack, and in the local
variables, are of the correct type for the instruction
 None of the following are allowed:
 Type errors





oop
Operand stack overflow or underflow
Access control violations (e.g., private fields and methods)
Reading of uninitialized variables
Use of uninitialized objects
Wild jumps
Preparation



oop
Allocating memory for the class (i.e static) variables and sets
them to default initial values.
Class variables are not initialized to their proper initial values
until the initialization phase - no java code is executed until
initialization.
The default values for the various types are shown below:
Resolution
 Replacing symbolic names for types, fields and
methods used by a loaded type with their actual
references.
 Symbolic references are resolved into direct
references by searching through the method
area to locate the referenced entity.
oop
Class Initialization
 Setting class variables to their proper initial values initial values desired by the programmer.
 Initialization of a class consists of two steps:
 Initializing its direct superclass (if any and if not
already initialized)
 Executing its own initialization statements
 The above imply that, the first class that gets
initialized is Object.
oop
JVM Memory Areas




oop
Each instance of the JVM has one method area, one heap,
and one or more stacks - one for each thread.
When JVM loads a class file, it puts its information in the
method area.
As the program runs, all objects instantiated are stored in
the heap.
The stack area is used to store activation records as a
program runs.
The Method Area
 Information about types
 Name, super class, modifiers…
 Constant pool
 Constants used by the type




Fields and methods information
Class (static) variables
Size is not fixed
Can be garbage collected
 Unreferenced classes can be unloaded
oop
JVM Stack vs. Heap
 Java stack




Used for method activation frames
One for each thread
No restrictions in its layout
Checked for overflow
 Java native stacks
 Native methods are used like other JVM methods, except they are
implemented in some other language (e.g. C)
 Can be used for things that cannot be handled well in Java, e.g.,
integrating legacy code written in other languages
 Native stacks are used to keep track of state of native methods
 Behave like the stack in C
 Java heap
 Used for allocating objects
 Managed by a garbage collector
 Each object is associated with a class stored in the method area
oop
Activation Frames
 One is pushed on the Java stack for each method
invocation
 The frame is popped when the invocation finishes
 A frame contains
 Local variables array
 Organized as an array
 Contains method’s parameters and local variables
 Size is determined at compile-time
 An operand stack
 Used for passing method arguments and results
 Only the top (few) words are accessed by any instruction
 Size is determined at compile-time
oop
Architectural Tradeoffs
 Program execution performance
 Interpreted languages are slower than running
compiled native code
 JIT improves performance
 Memory management control
 Garbage collection improve robustness but adds a level
of uncertainty to performance
 Platform independence
 Supports only the “lowest common denominator”
capabilities
oop