JVM Architecture – Understanding JVM Internals
JVM Architecture – Understanding JVM Internals
Every Java developer knows that bytecode will be executed by JRE (Java Runtime Environment). But many don’t know the fact that JRE is the implementation of Java Virtual Machine (JVM), which analyzes the bytecode, interprets the code and executes it. It is very important as a developer we should know the Architecture of JVM, this enables us to write code more efficiently. In this JVM architecture in Java with diagram article, we will learn more deeply about JVM architecture in Java and different components of a JVM.
What is a JVM in Java?
A Virtual Machine is a Software implementation of a Physical Machine, Java was developed with the concept of WORA ( Write Once Run Anywhere ) which runs on a VM. The compiler will be compiling the java file into a java .class file. The .class file is input to JVM which Loads and executes the class file. Below goes the Architecture of JVM.
JVM Architecture Diagram
How JVM works in Java?
As shown in the above architecture diagram JVM is divided into three main subsystems
1.ClassLoader Subsystem
2.Runtime Data Area
3.Execution Engine
1. ClassLoader Subsystem
Java’s dynamic class loading functionality is handled by the class loader subsystem. It loads, links and initializes the class when it refers to a class for the first time at runtime, not at compile-time. It performs three major functionality such as Loading, Linking, and Initialization.
1.1 Loading
Classes will be loaded by this component. BootStrap ClassLoader, Extension ClassLoader, Application ClassLoader are the three class loader which will help in achieving it.
The above Class Loaders will follow Delegation Hierarchy Algorithm while loading the class files.
1.2 Linking
1.Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get verification error.
2.Prepare – For all static variables memory will be allocated and assigned with default values.
3.Resolve – All symbolic memory references are replaced with the original references from Method Area.
1.3 Initialization
This is the final phase of Class Loading, here all static variable will be assigned with the original values and static block will be executed.
2. Runtime Data Area
Runtime Data Area is divided into 5 major components
1.Method Area – All the Class level data will be stored here including static variables. Method Area is one per JVM and it is a shared resource.
2.Heap Area – All the Objects and its corresponding instance variables and arrays will be stored here. Heap Area is also one per JVM since Method area and Heap area shares 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 as Stack Frame. All local variables will be created in the stack memory. Stack area is thread safe since it is not a shared resource. Stack Frame is divided into three sub-entities such as
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 act 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 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, separate native method stack will be created.
3. Execution Engine
The bytecode which is assigned to the Runtime Data Area will be executed by the Execution Engine. The Execution Engine reads the byte code and executes one by one.
1.Interpreter – Reads the bytecode, interprets it and executes it one by one. The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the interpreter is that when one method called multiple times, every time interpretation is required.
2.JIT Compiler – JIT Compiler neutralizes the disadvantage of the Interpreter ( a single method called multiple times, each time interpretation is required ), The Execution Engine will be using the help of Interpreter in converting but when it found repeated code it uses 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.
1.Intermediate Code generator – produces intermediate code
2.Code Optimizer – Code Optimizer is responsible for optimizing the intermediate code generated above.
3.Target Code Generator – Target Code Generator is responsible for Generating Machine Code/ Native Code
4.Profiler – Profiler is a special component, it is responsible for finding the hotspots (i.e) Used to identify whether the method is called multiple time or not.
3.Garbage Collector : Garbage Collector is a part of Execution Engine, it collects/removes the unreferenced objects. Garbage Collection can be triggered by calling “System.gc()”, but the execution is not guaranteed. Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup.
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 : It is a Collection of the Native Libraries which is required for the Execution Engine.
Every Java developer knows that bytecode will be executed by JRE (Java Runtime Environment). But many don’t know the fact that JRE is the implementation of Java Virtual Machine (JVM), which analyzes the bytecode, interprets the code and executes it. It is very important as a developer we should know the Architecture of JVM, this enables us to write code more efficiently. In this JVM architecture in Java with diagram article, we will learn more deeply about JVM architecture in Java and different components of a JVM.
What is a JVM in Java?
A Virtual Machine is a Software implementation of a Physical Machine, Java was developed with the concept of WORA ( Write Once Run Anywhere ) which runs on a VM. The compiler will be compiling the java file into a java .class file. The .class file is input to JVM which Loads and executes the class file. Below goes the Architecture of JVM.
JVM Architecture Diagram
How JVM works in Java?
As shown in the above architecture diagram JVM is divided into three main subsystems
1.ClassLoader Subsystem
2.Runtime Data Area
3.Execution Engine
1. ClassLoader Subsystem
Java’s dynamic class loading functionality is handled by the class loader subsystem. It loads, links and initializes the class when it refers to a class for the first time at runtime, not at compile-time. It performs three major functionality such as Loading, Linking, and Initialization.
1.1 Loading
Classes will be loaded by this component. BootStrap ClassLoader, Extension ClassLoader, Application ClassLoader are the three class loader which will help in achieving it.
1.BootStrap ClassLoader –Responsible for loading classes from the bootstrap classpath,nothing,but rt.jar. Highest priority will be given to this loader.
2.Extension ClassLoader – Responsible for loading classes which are inside ext folder (jre\lib)
3.Application ClassLoader –Responsible for loading Application Level Classpath, path mentioned Environment Variable etc.
The above Class Loaders will follow Delegation Hierarchy Algorithm while loading the class files.
1.2 Linking
1.Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get verification error.
2.Prepare – For all static variables memory will be allocated and assigned with default values.
3.Resolve – All symbolic memory references are replaced with the original references from Method Area.
1.3 Initialization
This is the final phase of Class Loading, here all static variable will be assigned with the original values and static block will be executed.
2. Runtime Data Area
Runtime Data Area is divided into 5 major components
1.Method Area – All the Class level data will be stored here including static variables. Method Area is one per JVM and it is a shared resource.
2.Heap Area – All the Objects and its corresponding instance variables and arrays will be stored here. Heap Area is also one per JVM since Method area and Heap area shares 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 as Stack Frame. All local variables will be created in the stack memory. Stack area is thread safe since it is not a shared resource. Stack Frame is divided into three sub-entities such as
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 act 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 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, separate native method stack will be created.
3. Execution Engine
The bytecode which is assigned to the Runtime Data Area will be executed by the Execution Engine. The Execution Engine reads the byte code and executes one by one.
1.Interpreter – Reads the bytecode, interprets it and executes it one by one. The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the interpreter is that when one method called multiple times, every time interpretation is required.
2.JIT Compiler – JIT Compiler neutralizes the disadvantage of the Interpreter ( a single method called multiple times, each time interpretation is required ), The Execution Engine will be using the help of Interpreter in converting but when it found repeated code it uses 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.
1.Intermediate Code generator – produces intermediate code
2.Code Optimizer – Code Optimizer is responsible for optimizing the intermediate code generated above.
3.Target Code Generator – Target Code Generator is responsible for Generating Machine Code/ Native Code
4.Profiler – Profiler is a special component, it is responsible for finding the hotspots (i.e) Used to identify whether the method is called multiple time or not.
3.Garbage Collector : Garbage Collector is a part of Execution Engine, it collects/removes the unreferenced objects. Garbage Collection can be triggered by calling “System.gc()”, but the execution is not guaranteed. Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup.
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 : It is a Collection of the Native Libraries which is required for the Execution Engine.
Comments
Post a Comment