JVM Internal Phases Exposed
?? ??Saral Saxena ??????
?11K+ Followers | Linkedin Top Voice || Associate Director || 15+ Years in Java, Microservices, Kafka, Spring Boot, Cloud Technologies (AWS, GCP) | Agile , K8s ,DevOps & CI/CD Expert
What is Virtual? This is not having physical existence
Virtual machine is a simple software program which simulates the functions of a physical machine. This is not having physical existence, but which can perform all operations like physical machines. Physical machine whatever it is doing, all those things we can able to do on the virtual machine. Best example of virtual machine is calculator in computer; it is worked like physical calculator.
Software based virtual machines
These virtual machines acts as run time engine to run a particular programming language applications. Examples of software based virtual machines are
1. JVM (Java Virtual Machine) acts as runtime engine to run Java applications
2. PVM (Parrot Virtual Machine) acts as runtime engine to run Perl applications
3. CLR (Common Language Runtime) acts as runtime engine to run .NET based applications
The first component in JVM is Class Loader Sub System
1. Class Loader Sub System
This system is responsible for loading .class file with 3 activities
- Loading
- Linking
- Initialization
1 Loading :-
Loading means read .class file from hard disk and store corresponding binary data inside method area of JVM. For each .class file JVM will store following information
1. Fully qualified name of class
2. Fully qualified name of immediate parent
3. Whether .class file represents class|interface|enum
4. Methods|Constructors|Variables information
5. Modifiers information
6. Constant Pool information
After loading the class file and store inside method area, immediately JVM will perform one activity i.e., create an object of type java.lang.Class in method area.
Created object is not student object or customer object. It is a predefined class “Class†object that is presently in java.lang package. The created object is represents either student class binary information or customer class binary information.
2 Linking :-
After “loading†activity JVM immediately perform Linking activity. Linking once again contain 3 activities,
- Verification
- Preparation
- Resolution
Java language is the secure language. Through java spreading virus, malware these kind of this won't be there. If you execute old language executable files (.exe) then immediately we are getting alert message saying you are executing .exe file it may harmful to your system.
Byte Code Verifier /verification : - But in java .class files we never getting these alert messages. What is the reason is inside JVM a special component is there i.e., Byte Code Verifier. This Byte Code Verifier is responsible to verify weather .class file is properly formatted or not, structurally correct or not, generated by valid compiler or not. If the .class file is not generated by valid compiler then Byte Code Verifier raises runtime error java.lang.VerifyError. This total process is done in verification activity.
preparation :- phase, JVM will allocate memory for class level static variables and assigned default values.
E.g. For int ---> 0, For double ---> 0.0, For boolean ---> false
Here just default values will be assigned and original values will be assigned in initialization phase.
Resolution. :- It is the process of replacing all symbolic references used in our class with original direct references from method area.
Initialization In Initialization activity, for class level static variables assigns original values and static blocks will be executed from top to bottom.
Varies Memory Area/Runtime Data Area :-
1) Method Area :-
- The method area is used to store the class data and method data including static methods.
- There is only one method area per JVM and it is a shared resource.
2) Heap Area :
- The Heap Area is used to store the objects and their corresponding instance variables and arrays.
- There is only one Heap area per JVM, hence Method and Heap area are shared among resources so the data stored is not thread safe.
3) Stack Area :
- For every new thread a new stack will be created,
- All Local variables will be created and stored in stack area.
- Since it is not a shared resource, so it is thread safe.
4) PC Registers :-
- PC(Program Counter) Register contains the address of the JVM instruction, currently being executed.
5) Native Method Stack :-
- It contains all the Native method used in the application.
Execution Engine :-
The byte-code which is assigned to Runtime data area will be executed here.
1) Interpreter :
- It interprets the bytecode faster and but executes code slowly.
- The disadvantage of the interpreter is that when one method is called multiple times, every time a new interpretation is required.
2) JIT Compiler :
- It is used to improve the performance.
- The Interpreter is used in converting the bytecode, but when it finds the repeated code it will use the JIT compiler.
- The JIT Compiler 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.