[go: up one dir, main page]

0% found this document useful (0 votes)
73 views7 pages

? in-Depth Guide to JVM Architecture ?

Uploaded by

BADR EL KHALYLY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views7 pages

? in-Depth Guide to JVM Architecture ?

Uploaded by

BADR EL KHALYLY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JVM Architecture

JVM (Java Virtual Machine) is a software that interprets and executes Java code.

------------------Hierarchy ---------------

o Class Loader
▪ Loading
• Bootstrap class loader
o Load – core java libraries
o From – jre/lib/rt.jar
• Extension Class Loader
o Load – java extended libraries(3rd Parties)
o From – jre/lib/ext and java.ext.dir
• Application or System Class loader
o Load – application classes
o From – ClassPath specifies by User / CodeBase
▪ Linking
• Verify- byte code verifier
• Prepare – allocate memory for static member and load default values.
• Resolve – resolve the symbols in the byteCodes
▪ Initialization- executing the static members and assign the original value.
o Runtime Memory Area
▪ Method area- class level data – method, field, staticvars.
▪ Heap area – Objects and corresponding instances ,Arrays, variables
▪ Stack area – per Thread – local vars, Object refs, Method calls
▪ Pc registers – store current thread execution operation data
▪ Native stack – per Thread – native method info
o Execution Engine
▪ Interpreter – interpret the bytecode (having slow execution)
▪ JIT compiler – for repeated call it will once convert to native it reuse(faster execution)

-------oooooooooo----------

• The JVM consists of three main components:


1. the class loader,
2. the runtime data area, and
3. the execution engine.

▪ The class loader is responsible for loading classes into the JVM. When you run a Java program,

the class loader searches for the necessary classes and loads them into the runtime data area.
▪ The runtime data area is where the JVM stores data during program execution. It consists of

several different areas, including the method area (where class and method information is

stored), the heap (where objects are stored), and the stack (where method calls and local

variables are stored).

▪ The execution engine is responsible for executing the bytecode (compiled Java code) that is

loaded into the JVM. The execution engine reads the bytecode and performs the necessary

operations to carry out the program’s instructions.

▪ The JVM also includes a garbage collector, which automatically frees up memory that is no

longer being used by the program.

class loader,
ClassLoader in Java is a class that is used to load class files in Java. Java code is
compiled into a class file by javac compiler and JVM executes the Java program,
by executing byte codes written in the class file. ClassLoader is responsible for
loading class files from file systems, networks, or any other source.
Java class loaders are used to load classes at runtime.
There is three default class loader used in Java,
▪ Bootstrap class loader.
▪ Extension class loader.
▪ System or Application class loader.

Bootstrap class loader…


The Bootstrap Class Loader is the first class loader to load classes when the JVM starts up. It loads the
core Java API classes (such as java.lang.String) from the rt.jar file located in the Java runtime
environment. It is implemented in native code and is responsible for loading the core Java libraries,
which are required for the JVM to function. The Bootstrap Class Loader is an integral part of the JVM
and cannot be replaced or modified by the user.

▪ The bootstrap class loader is responsible for loading standard JDK class files
from rt.jar and it is the parent of all class loaders in Java.
▪ Bootstrap ClassLoader - JRE/lib/rt.jar
Extension class loader.
The Extension Class Loader is responsible for loading classes from the extensions directory, which
contains JAR files with third-party extensions to the Java platform. The extensions directory is
specified by the java.ext.dirs system property, and the Extension Class Loader loads the classes from
JAR files located in this directory. For example, if you use a third-party library that extends the
functionality of the Java platform, the Extension Class Loader will load the classes from the JAR file
containing that library.
▪ Extension ClassLoader - JRE/lib/ext or any directory denoted by java.ext.dirs

System or Application class loader


The System Class Loader, also known as the Application Class Loader, is responsible for loading
classes from the classpath specified by the user. It is implemented in Java and is responsible for
loading the classes from the application codebase. The classpath is a list of directories and JAR files
where the System Class Loader searches for classes. For example, when you run a Java program using
the “java” command, the System Class Loader loads the classes from the specified classpath.

• Application ClassLoader - CLASSPATH environment variable, -classpath or -


cp option, Class-Path attribute of Manifest inside JAR file.
Application class loader is a child of Extension ClassLoader and its implemented
by sun.misc.Launcher$AppClassLoader class. Also, except for the Bootstrap class
loader, which is implemented in the native language mostly in C, all Java class loaders
are implemented using java.lang.ClassLoader.

Runtime Data Area:


The runtime data area is the memory area where data is stored during program execution. It is divided
into several different areas, each with its own purpose.

When this program is executed, the JVM will allocate memory in the runtime data area to store the

program’s data. Here’s how the memory might be allocated:

• Method area: The method area is where the JVM stores class and method information. In
our example program, the method area would store information about
the ExampleProgram class and its main method.

• Heap: The heap is where objects are stored. In our example program, the String object

containing the message "Hello, world!" would be stored on the heap.

• Stack: The stack is where method calls and local variables are stored. In our example
program, the main method would be added to the stack when it is called.
The int variable a would also be allocated on the stack, along with a reference to

the String object containing the message.


• Here’s a simplified diagram showing how memory might be allocated in the runtime data

area:

Method area:
- ExampleProgram class
- main method

Heap:
- String object ("Hello, world!")

Stack:
- main method
- int variable a (value = 10)
- String reference variable message (points to the String object on the heap)

#1.Class Loader SubSystem :

Java Dynamic Class loading functionality is handled by class loader subsystem. “Load -> Link -> Initialize

the class file” for the first time at runtime.

1.1. Loading : Classes will be loaded by this component.

1. Bootstrap Class Loader — loads class from bootstrap class path (rt.jar -high priority)

2. Extension Class Loader — loads the class which included in (jre/lib).

3. Application Class Loader — Loads application level class.

1.2 Linking : Performs the Verification,Preparation and Resolution on Class loaded.

1. Verify : Bytecode verifier will verify whether the generated bytecode is proper or not , else we will

get Verification Error(java.lang.VerifyError)


2. Prepare : For all static variables in bytecode , memory will be allocated and default values will be

loaded.

3. Resolve(Optional): Symbolic References of class will replaced with original References from

method area.

1.3 Initialization : Final phase of class loader , here all static variables will be assigned original values

& static block gets executed.

#2. Runtime Data Area : (JVM Memory) — divided into 5 components

1. Method Area : this will store all the class level data (fields, method,static variable). Only one method

area per JVM.

2. Heap Area : all the objects & corresponding instance , variable , array will be stored. One Heap per

JVM’=

o where Heap & Method Area are not thread safe it shared resource for JVM

3. Stack Area : For every thread new stack at runtime will be created. for every method call , one entry

will be added in stack called stack frame.


▪ Local Variable will be stored in stack memory
▪ Thread safe
▪ Stack Frame is divide into three sub entities:
• Local Variable Array: related to local method , variables are invloved
• Operand Stack : Intermediate Operation — acts as runtime workspace to
perform operation
• Frame Data : all symbols corresponding to method will be stored , in case of any
exception , catch block information will be maintained.

4. PC Registers: Each Thread will have register which stores the current executing operation.(once

each instruction updating , PC register also will update next instruction)

5. Native Method Stack: Holds native method information for every thread

#3. Execution Engine : Byte code which is assigned in Runtime data area will be executed.

1. Interpreter: Interprets the bytecode faster but execution slowly.(one method is called

multiple times it will create new Interpretation is required).

2. JIT Compiler : neutralize the disadvantage of interpreter.whenever it finds repeated code it

uses JIT Compiler.

It will compile the bytecode into native code (Native code will used directly for Repeated

method calls).

You might also like