JVM

21
By, PRITY KUMARI 205111037 NIT-Trichy(MCA)

description

 

Transcript of JVM

Page 1: JVM

By,PRITY KUMARI

205111037NIT-Trichy(MCA)

Page 2: JVM

JVM is an interpreter for bytecode. JVM needs to be implemented on each

platform. Enhance portability. Assure security. Encourage Fast execution of the

program. Def : JVM is a component of the Java

system that interprets and executes the instructions in our class files.

Page 3: JVM

Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format.

The class file format precisely defines the representation of a class or interface, including details such as byte ordering that might be taken for granted in a platform-specific object file format.

Page 4: JVM

Like the Java programming language, the Java Virtual Machine operates on two kinds of types: primitive types and reference types.

There are, correspondingly, two kinds of values that can be stored in variables, passed as arguments, returned by methods, and operated upon: primitive values and reference values.

The instruction set of the Java Virtual Machine distinguishes its operand types using instructions intended to operate on values of specific types.

For instance, iadd, ladd, fadd, and dadd

Page 5: JVM

numeric types, the boolean type returnAddress type1)Numeric typesThe integral types are: Byte(8-bit signed two's-complement integers) Short (16-bit signed two's-complement

integers) Int(32-bit signed two's-complement integers) Long(64-bit signed two's-complement

integers) Char(16-bit unsigned )and default value is

the null code point ('\u0000')

Page 6: JVM

2) Floating-point: float, whose values are elements of the float value

set and whose default value is positive zero double, whose values are elements of the double

value and whose default value is positive zero

3)Boolean-type: encode the truth values true and false, and the

default value is false.

4)returnAddress Type: The values of the returnAddress type are pointers

to the opcodes of Java Virtual Machine instructions. Of the primitive types, only the returnAddress

type is not directly associated with a Java programming language type.

Page 7: JVM

There are three kinds of reference types: class types array types interface types. Their values are references to dynamically

created class instances, arrays, or class instances or arrays that implement interfaces, respectively.

A reference value may also be the special null reference, a reference to no object, which will be denoted here by null

The Java Virtual Machine specification does not mandate a concrete value encoding null.

Page 8: JVM

FIG: Memory Configuration of JVM

Page 9: JVM

The JVM defines various run-time data areas that are used during execution of a program. Some of these data areas are created on JVM start-up and are destroyed only when the Java Virtual Machine exits.

Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.

Page 10: JVM

1)The pc Register: The JVM can support many threads of execution at once Each Java Virtual Machine thread has its own pc (program

counter) register.

2) JVM Stacks: Each JVM thread has a private JVM stack, created at the

same time as the thread. It holds local variables and partial results, and plays a part in

method invocation and return. Because the JVM stack is never manipulated directly except

to push and pop frames, frames may be heap allocated. The memory for a JVM stack does not need to be contiguous. Throws two exceptions

- OutOfMemoryError. - StackOverflowError.

Page 11: JVM

3) Heap The JVM has a heap that is shared among all Java

Virtual Machine threads. The heap is the run-time data area from which

memory for all class instances and arrays is allocated. The heap is created on virtual machine start-up. Throws one exception

- OutOfMemoryError

4) Method Area The method area is analogous to the storage area for

compiled code of a conventional language or analogous to the "text" segment in an operating system process.

It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods used in class and instance initialization and interface initialization.

Throws one exception - OutOfMemoryError

Page 12: JVM

5) Run-Time Constant Pool It contains several kinds of constants, ranging from

numeric literals known at compile-time to method and field references that must be resolved at run-time.

The run-time constant pool serves a function similar to that of a symbol.

6) Native Method Stacks

An implementation of the Java Virtual Machine may use conventional stacks, called "C stacks," to support native methods

Java Virtual Machine implementations that cannot load native methods and that do not themselves rely on conventional stacks need not supply native method stacks.

If supplied, native method stacks are typically allocated per thread when each thread is created.

Page 13: JVM

Figure 2: Content of Memory Blocks at runtime.

Page 14: JVM

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 it loads, the JVM must store the following information in the method area: The fully qualified name of the type Whether the type is a class or an interface The type's modifiers ( public, abstract, final, etc) Method info: name, return type, number & types of

parameters, modifiers, bytecodes, size of stack frame and exception table.

Page 15: JVM

The end of the loading process is the creation of an instance of java.lang.Class for the loaded type.

The purpose is to give access to some of the information captured in the method area for the type, to the programmer.

Some of the methods of the class java.lang.Class are:

Note that for any loaded type T, only one instance of java.lang.Class is created even if T is used several times in an application.

public String getName()public Class getSupClass()public boolean isInterface()public Class[] getInterfaces()public Method[] getMethods()public Fields[] getFields()public Constructor[] getConstructors()

Page 16: JVM

The next process handled by the class loader is Linking. This involves three sub-processes: Verification, Preparation and Resolution

Example of some of the things that are checked at verification are:

Every method is provided with a structurally correct signature

Every instruction obeys the type discipline of the Java language

Every branch instruction branches to the start not middle of another instruction

Page 17: JVM

In this phase, the JVM allocates memory for the class (i.e static) variables and sets them to default initial values.

Note that 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 18: JVM

Resolution is the process of replacing symbolic names for types, fields and methods used by a loaded type with their actual references.

Symbolic references are resolved into a direct references by searching through the method area to locate the referenced entity.

For the class below, at the loading phase, the class loader would have loaded the classes: TestClassClass, String, System and Object.

The names of these classes would have been stored in the constant pool for TestClassClass.

public class TestClassClass{ public static void main(String[] args){ String name = new String(“Ahmed”); Class nameClassInfo = name.getClass(); System.out.println("Parent is: “ + nameClassInfo.getSuperclass()); }}

Page 19: JVM

After a class is loaded, linked, and initialized, it is ready for use. Its static fields and static methods can be used and it can be instantiated.

When a new class instance is created, memory is allocated for all its instance variables in the heap.

Memory is also allocated recursively for all the instance variables declared in its super class and all classes up is inheritance hierarchy.

All instance variables in the new object and those of its superclasses are then initialized to their default values.

Finally, the reference to the newly created object is returned as the result.

Page 20: JVM

Rules for processing a constructor: Assign the arguments for the constructor to its parameter

variables. If this constructor begins with an explicit invocation of

another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively.

If this constructor is for a class other than Object, then it will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively.

Initialize the instance variables for this class with their proper values.

Execute the rest of the body of this constructor.

Page 21: JVM