Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM...

22
oo JVM JVM in Context in Context
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    240
  • download

    4

Transcript of Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM...

Page 1: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

JVMJVMin Contextin Context

Page 2: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

What’s a JVMWhat’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.

Page 3: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Platform independencePlatform 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

Page 4: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

JVM in Java ArchitectureJVM in Java Architecture Java’s architecture main technologies:

The Java programming languageSource files

The Java class file formatCompiled files

The Java APIProvide access to system resources

The Java virtual machineRuns the class files

Page 5: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

The Java Programming EnvironmentThe Java Programming Environment

Compile time environment run time environment

A.JavaA.Java B.JavaB.Java C.JavaC.Java

JavaCompiler

A.classA.class B.classB.class C.classC.class

A.classA.class B.classB.class C.classC.class

JavaVirtual

Machine

Object classObject class String classString class

Page 6: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Same Program, Different PlatformsSame Program, Different Platforms

JavaProgram

JavaProgram

Java Platformfor

Linux

Java Platformfor

Linux

Linux BoxLinux Box

JavaProgram

JavaProgram

Java Platformfor

Win32

Java Platformfor

Win32

PC running Windows XP

PC running Windows XP

JavaProgram

JavaProgram

Java Platformfor your

Television

Java Platformfor your

Television

YourTelevision

YourTelevision

JavaProgram

JavaProgram

Java Platformfor yourToaster

Java Platformfor yourToaster

YourToaster

YourToaster

Page 7: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

The JVM RolesThe 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.

Page 8: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Basic JVM ComponentsBasic JVM Components

The Java Virtual Machine

Classloader

Executionengine

Host operating system

ProgramClassfiles

The JavaAPI’s

class files

Native methods invocation

Page 9: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

The Java Class File 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:

CompilerCompiler

CompilerCompiler

CompilerCompiler

Program in JavaLang.

Program in JavaLang.

Program in other

Lang.

Program in other

Lang.

Program in JavaLang.

Program in JavaLang.

JavaJava

classclass

filesfiles

JavaJava

classclass

filesfiles

OtherBinaryformat

OtherBinaryformat

Page 10: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

The Java APIThe 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

JavaProgram

JavaProgram

Java Methods (Java API)

Native methods (dynamic libraries)

Host operating system

Page 11: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

JVM Major Subsystems and Memory JVM Major Subsystems and Memory Areas.Areas.

Page 12: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

The Class Loader SubsystemThe Class Loader Subsystem

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.

Page 13: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Class LoadingClass 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.

Page 14: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Linking : VerificationLinking : 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 Operand stack overflow or underflow Access control violations (e.g., private fields and methods) Reading of uninitialized variables Use of uninitialized objects Wild jumps

Page 15: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

PreparationPreparation 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:

Page 16: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

ResolutionResolution

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.

Page 17: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Class InitializationClass 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.

Page 18: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

JVM Memory AreasJVM Memory Areas

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.

Page 19: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

The Method AreaThe 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

Page 20: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

JVM Stack vs. HeapJVM 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

Page 21: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Activation FramesActivation 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 arrayOrganized as an arrayContains method’s parameters and local variablesSize is determined at compile-time

An operand stackUsed for passing method arguments and resultsOnly the top (few) words are accessed by any instructionSize is determined at compile-time

Page 22: Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.

oop

Architectural TradeoffsArchitectural 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