0% found this document useful (0 votes)
23 views

JVM Final Slides

The Java Virtual Machine (JVM) is an abstract computing machine that enables the execution of various programming languages, not just Java, by providing a secure and platform-independent environment. It consists of multiple components, including method area, heap, and stacks, and utilizes a classloader subsystem to load and manage class files. The JVM also includes a runtime data area for executing bytecode, a garbage collector for memory management, and supports various data types and instruction sets for efficient program execution.

Uploaded by

handf9766
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

JVM Final Slides

The Java Virtual Machine (JVM) is an abstract computing machine that enables the execution of various programming languages, not just Java, by providing a secure and platform-independent environment. It consists of multiple components, including method area, heap, and stacks, and utilizes a classloader subsystem to load and manage class files. The JVM also includes a runtime data area for executing bytecode, a garbage collector for memory management, and supports various data types and instruction sets for efficient program execution.

Uploaded by

handf9766
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

What is a Java Virtual Machine?

• JVM is an abstract computing machine


• Like an actual computing machine, it has an instruction set and manipulates
various memory areas at run time
• A JVM enables a set of computer software programs and data
structures to use a virtual machine model for the execution of other
computer programs and scripts
• Not just Java, JVM now supports many languages
• Ada, C, LISP, Python
Why a Virtual Machine?

•The Java platform was initially developed to address the problems of


building software for networked consumer devices
•It was designed to support multiple host architectures and to allow
secure delivery of software components
•To meet these requirements, compiled code had to survive transport
across networks, operate on any client, and assure the client that it
was safe to run
•"Write Once, Run Anywhere"
Introduction to the JVM (Cont’d)
• 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


Classloader

• Classloader is a subsystem of JVM which is used to load class files. Whenever we run the
java program, it is loaded first by the classloader. There are three built-in classloaders in Java.
• Bootstrap ClassLoader: This is the first classloader which is the super class of Extension
classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like
java.lang package classes, java.net package classes, java.util package classes, java.io package
classes, java.sql package classes etc.
• Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of
System classloader. It loades the jar files located inside $JAVA_HOME/jre/lib/ext directory.
• System/Application ClassLoader: This is the child classloader of Extension classloader. It
loads the classfiles from classpath. By default, classpath is set to current directory. You can
change the classpath using "-cp" or "-classpath" switch. It is also known as Application
classloader.
Class Loading Process
• 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
• The fully qualified name of the type's direct superclass or if the type is an interface, a list of its direct super
interfaces .
• Whether the type is a class or an interface
• The type's modifiers ( public, abstract, final, etc)
• Constant pool for the type: constants and symbolic references.
• Field info : name, type and modifiers of variables (not constants)
• Method info: name, return type, number & types of parameters, modifiers, bytecodes, size of stack frame and
exception table.
Introduction to the JVM (Cont’d)

Figure 2: Content of Memory Blocks at runtime.


The 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

Figure 3: Class loading process.


1.2 Linking

• Verify – Bytecode verifier will verify whether the generated bytecode


is proper or not if verification fails we will get the verification error.
• Prepare – For all static variables memory will be allocated and
assigned with default values.
• Resolve – All symbolic memory references are replaced with the
original references from Method Area.
Preparation
• 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:
Resolution
• 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.
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());
}
}

• The names of these classes would have been stored in the constant pool for TestClassClass.
• In this phase, the names are replaced with their actual references.
Class Initialization
• This is the process of setting class variables to their proper initial values - initial values desired by the programmer .

class Example1 {
static double rate = 3.5;
static int size = 3*(int)(Math.random()*5);
...
}
• 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.
• Note that static final variables are not treated as class variables but as constants and are assigned their values at compilation.

class Example2 {
static final int angle = 35;
static final int length = angle * 2;
...
}
1.3 Initialization

• This is the final phase of ClassLoading; here, all static variables will be
assigned with the original values, and the static block will be
executed.
2. Runtime Data Area
The Runtime Data Area is divided into five major components:
1. Method Area – All the class-level data will be stored here, including static variables. There is only one
method area per JVM, and it is a shared resource.
2. Heap Area – All the Objects and their corresponding instance variables and arrays will be stored here.
There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple
threads, the data stored is not thread-safe.
3. Stack Area – For every thread, a separate runtime stack will be created. For every method call, one entry
will be made in the stack memory which is called Stack Frame. All local variables will be created in the
stack memory. The stack area is thread-safe since it is not a shared resource. The Stack Frame is divided
into three subentities:
1. Local Variable Array – Related to the method how many local variables are involved and the
corresponding values will be stored here.
2. Operand stack – If any intermediate operation is required to perform, operand stack acts as runtime
workspace to perform the operation.
3. Frame data – All symbols corresponding to the method is stored here. In the case of any exception,
the catch block information will be maintained in the frame data.
4. PC Registers – Each thread will have separate PC Registers, to hold the address of current executing
instruction once the instruction is executed the PC register will be updated with the next instruction.
5. Native Method stacks – Native Method Stack holds native method information. For every thread, a
separate native method stack will be created.
• The bytecode, which is assigned to the Runtime Data Area, will be executed by the
Execution Engine. The Execution Engine reads the bytecode and executes it piece by
piece.
• Interpreter – The interpreter interprets the bytecode faster but executes slowly. The
disadvantage of the interpreter is that when one method is called multiple times,
every time a new interpretation is required.
• JIT Compiler – The JIT Compiler neutralizes the disadvantage of the interpreter. The
Execution Engine will be using the help of the interpreter in converting byte code,
but when it finds repeated code it uses the JIT compiler, which compiles the entire
bytecode and changes it to native code. This native code will be used directly for
repeated method calls, which improve the performance of the system.Intermediate
Code Generator – Produces intermediate code
• Code Optimizer – Responsible for optimizing the intermediate code generated above
• Target Code Generator – Responsible for Generating Machine Code or Native Code
• Profiler – A special component, responsible for finding hotspots, i.e. whether the
method is called multiple times or not.
• Garbage Collector: Collects and removes unreferenced objects.
Garbage Collection can be triggered by calling System.gc(). but the
execution is not guaranteed. Garbage collection of the JVM collects
the objects that are created.
• Java Native Interface (JNI): JNI will be interacting with the Native
Method Libraries and provides the Native Libraries required for the
Execution Engine.
• Native Method Libraries: This is a collection of the Native Libraries,
which is required for the Execution Engine.
JVM Instruction Set Architecture
• Instructions
• A Java virtual machine instruction consists of a one-byte opcode specifying
the operation to be performed, followed by zero or more operands supplying
arguments or data that are used by the operation
• Operands are not required, there are many instructions that consist of only
the opcode
• One-byte instructions allow for up to 256 instructions but only about 200 are
used in class files, leaving room for more instructions to be added
• Each instruction has a mnemonic name which is mapped to the binary one-
byte opcode
JVM Instruction Set Architecture
• Instruction Format
• The mnemonic operation names often include the data type followed by the
operation name
• iadd, ladd, fadd, dadd
• int, long, float, double
• JVM supports conversion operations that convert from one data type to
another, these include both data types in the operation name
• i2l, i2f, i2d, l2f, l2d, f2d
Operation Types
• The JVM ISA is a CISC architecture, thus having many instructions
• They can be classified into broad groups
• Load and store
• Arithmetic and logic
• Type conversion
• Object creation and manipulation
• Operand stack management
• Control transfer
• Method invocation and return
Operation Types
• Load and store
• Used to move values from local variable array or heap to the operand stack
• iload, istore
•Arithmetic and logic
• JVM supports addition, subtraction, division, multiplication, remainder, negation, increment
• irem, idiv, iinc
• Type conversion
• Allows converting from one primitive data type to another
• i2l, i2f, i2d, l2f, l2d, f2d
• Object creation and manipulation
• Instantiating objects and manipulating fields
• new, putfield
• Operand stack management
• swap, dup2
• Control transfer
• ifeq, goto
•Method invocation and return
• invokespecial, areturn
JVM Data Types
• The Java virtual machine operates on two kinds of types: primitive
types and reference types
• Integral Types:
• 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 integers representing Unicode characters
JVM Data Types
JVM Data Types
• Floating Point Types:
• Float - values are elements of the float value set (typically 32-bit single-
precision but may vary with implementation)
• Double - values are elements of the double value set(64-bit double-precision)
• Boolean - values true and false
• JVM has very little support for the Boolean data type
• Boolean variables in a Java program are compiled to use values of the JVM int
data type
• returnAddress - are pointers to the opcodes of Java virtual machine
instructions
JVM Data Types
• Three kinds of reference types
• Class types
• Array types
• Interface types
• These reference dynamically created classes, arrays, or interface
implementations
• Can be set to null when not referencing anything and then cast to any
type
JVM Data Types
• The basic unit of size for data values in the Java virtual machine is the
word
• a fixed size chosen by the designer of each Java virtual machine
implementation
• The word size must be large enough to hold a value of type byte,
short, int, char, float, returnAddress, or reference
• at least 32 bits
JVM Runtime Data Areas
• Since JVM is a virtual machine it doesn’t have any physical registers ,
instead it defines various runtime data areas that are used during
execution of a program
• One of the areas defined is the program counter register
• Each thread of control has its own PC register
• The register is wide enough to contain a returnAddress or a native pointer on
the specific platform
JVM Runtime Data Areas
• JVM Stack
• Each thread gets its own JVM stack when it is created
• Stacks store frames which hold data and play a role in method invocation and
return
• The actual memory for a JVM stack does not need to be contiguous
• The stack can be either of a fixed size or dynamically contracted and
expanded as needed
JVM Runtime Data Areas
• JVM Frames
• A frame is used to store data and partial results, as well as to perform dynamic linking , return values
for methods, and dispatch exceptions
• A new frame is created each time a method is invoked and destroyed when the method is completed
• Only one frame, for the executing method, is active at any point
• Each frame contains a local variable array
• Local variables can store primitive or reference data types
• Variables are addressed by index, starting from zero
• Data types long and double occupy two consecutive local variables
• Frames also contains an operand stack
• Last-in-first-out (LIFO)
• JVM loads values from local variables or fields onto the stack
• Then JVM instructions can take operands from the stack, operate on them, and the push the result back onto the
stack
• The operand stack size is fixed at compile time based on method associated with the frame
JVM Operand Stack

• Code:
• iload_0 // push the int in local variable 0
• iload_1 // push the int in local variable 1
• iadd // pop two ints, add them, push result
• istore_2 // pop Int, store into local variable 2
JVM Runtime Data Areas
• JVM Heap
• The heap is a data area shared by all JVM threads
• Memory from the heap is allocated for instances of classes and arrays
• Can be either of fixed size or dynamic
• Does not to be in contiguous memory space
• Maintained by an automatic storage management system or garbage collector
JVM Runtime Data Areas
• Method Area
• The method area is also shared among all JVM threads
• It stores per-class structures
• such as the runtime constant pool, field and method data, code for methods and
constructors, including the special methods used in class and instance initialization
• The method area is logically part of the heap, but depending on the
implementation it may or may not be garbage collected or compacted
JVM Runtime Data Areas
• Runtime Constant Pool
• The runtime constant pool is a per-class runtime representation of the
constant pool table in a class file
• It contains numeric constants as well as method and field references that are
resolved at runtime
• This is similar to a symbol table for a conventional programming language,
although it stores a wider range of data
JVM Addressing Modes
• JVM supports three addressing modes
• Immediate addressing mode
• Constant is part of instruction
• Indexed addressing mode
• Accessing variables from local variable array
• Stack addressing mode
• Retrieving values from operand stack using pop
JVM Method Calls

• Sample Code
int add12and13() {
return addTwo(12, 13);
}

• Compiles to
Method int add12and13()
0 aload_0 // Push local variable 0 (this)
1 bipush 12 // Push int constant 12
3 bipush 13 // Push int constant 13
5 invokevirtual #4 // Method Example.addtwo(II)I
8 ireturn // Return int on top of operand stack; it //is the
int result of addTwo()

You might also like