Internal working of JVM
JVM is an abstract computing machine or Virtual machine which is a platform independent environment and converts the bytecodes into machine language and help to execute the program.We will study about its internal working.The explanation is divided in 4 parts as given below.
- Compiler usage
- Class loader Subsystem
- Memory Areas
- Execution Engine
1. Compiler Usage: We write java program in simple files with .java extension. Compiler’s job is to convert the .java file into .class files which is nothing but the bytecodes(as shown below). The input from this part goes to class loader subsystem.
2. Class loader Subsystem: The class loader subsystem is divided into 3 main parts as follows.
- Class loader
- Linking
- Initialization
Class Loader: Class loader performs the loading of .class files. There are 3 types of class loaders
- Bootstrap Class loader
- Extension class loader
- Application class loader
The priority is given to the Bootstrap class loader to load the class. If it is unable to find the class files, then job is transferred to ‘Extension Class loader’ and if the same is not done by ‘Extension class loader’, Application class loader takes the charge.
Linking: Verification of bytecodes, default values assignments to static variables etc. This section also contains 3 subareas as given below
- Verify: It is responsible to verify the bytecodes that it is coming from a valid source or there is no error etc. It helps to make the process more secured.
- Prepare: It allocates the default values to static variables.
- Resolve: All symbolic reference are resolved with original reference coming from method area.
Initialization: Original values to static variables are assigned and static blocks are executed.
3. Memory Areas: There are various memory areas as defined below.
- Method Area
- Heap Area
- Stack Area
- PC Register Area
- Native method Area
Method Area: All class data and static variables are stored here.
Heap Area: All the object data,instance variable, arrays etc. are stored here.
Stack Area: It contains threads. A thread provides an independent path for execution of the program. Many threads can run parallel within a program. For every thread, one run time stack is present. All local variables and method calls are stored here per thread.
PC Register: For every thread, there is one PC registration created to hold the address of next executing instruction.
Native method Area: This will stored native method information.
4. Execution Engine: It is simply responsible to execute the programs i.e .class file. It contains the following components mentioned below.
- Interpreter
- JIT (Just In Time) compiler
- Profiler
- Garbage collector
Interpreter: It read, interpret and execute the program line by line.Every java program should be interpreted at least once. I
JIT(Just In Time) Compiler: It is responsible to convert the bytecodes into machine language which is understood by any platform. It is mainly used for the methods which are repetitive in nature.JIT compiler runs the program much faster.
Profile: Its job is to inform JIT about the repetitive methods.
Garbage Collector: It is used to release the memory which is occupied by unused objects.