JVM (Java Virtual Machine)
In general, we can say JVM is a program which takes byte code(.class file[the compilation of .java file]) and converts into machine instruction then providing an execution engine that is JAVA interpreter named as java which executes source code line by line.
Now we will discuss JVM in detail.
Assume that we have a java program(.java file). the java compiler javac compiles and generates .class file which is called as byte code. This byte code is the input of JVM. That means JVM takes .class file, then load it and execute it.
JVM consists of 3 modules, such as:
- ClassLoader Subsystem
- Memory area
- Execution engine
- ClassLoader Subsystem consist 3 activities, such as Loading, Linking and Initializing. That means it is responsible for loading, linking and initializing the .class file.
- In loading activity, there are 3 types of loaders, such as the bootstrap class loader, extension class loader, and application class loader.
- The bootstrap class loader is responsible for loading class from the bootstrap class path(rt.jar). All the core java API classes are loaded in the bootstrap class loader.
- Extension class loader is responsible for loading the classes present inside the home folder.
- Application class loader is responsible to load application-level classpath such as environment variable or environmental classpath.
- Among these 3 class loaders, bootstrap class loader having the highest priority. If the bootstrap class loader is unable to load the .class file, then extension class loader will take care for loading, if the extension class loader is unable to load then application loader will responsible to load the .class file.
- Then it will communicate with linking activity. In linking activity, there are 3 types of features, such as verify, prepare and resolve.
- After loading, immediately verification will start. Whether it has generated byte code properly or not. If verification fails, then verify error will generate.
- Then prepare for a static variable, the memory will be allocated and assign with default values.
- Then resolve represents that all the symbolic references are replaced with original references from method area.
- The original values for the static variable will be placed in initialization. That means static blocks will be executed.
- After initialization, class loading is successfully completed.
Then class loader subsystem communicates with various memory area inside JVM.
There are 5 memory areas available inside JVM, such as method area, heap area, stack area, PC register, native method stack area.
- After loading .class file successfully, it is dumped in method area. Class level data including static variable will be stored in method area.
- Then the next memory area is heap area which is very important for the programmer point of view. Object data, instance variable, array will be stored in heap.
- Then the next memory area is stack area. For every thread, a separate runtime stack will be created including local variable. In each stack, each entry(method call) in the stack is called as stack frame. The stack frame consists 3 parts such as local variable array, operand stack and frame data. The local variable array represent how many local variables are there and their corresponding values. Then the operand stack represents any intermediate operation required to be performed which are accessed at runtime. Then frame data contains all the symbols which are used in methods.
- Then the next memory area is PC registers. For each thread PC register will be created to hold address of next executing instruction. For exa, PC register for thread T1, PC register for thread T2 etc.
- Then last memory area inside JVM is native method stack area. Here for every thread a separate runtime stack will be there such as T1,T2,…Tn. It hold native method information.
The data which is stored in method area and heap area is not thread-safe whereas the data stored in the stack area and PC registers are thread-safe.
- The execution engine is responsible to execute the program.
- There are 2 JAVA execution engines like JAVA Interpreter and JIT compiler.
- JAVA Interpreter read each instruction line by line and execute it.
- If the method is repeated, JIT compiler executes for better performance.
- JIT compiler consists of different components, such as intermediate code generator, code optimizer and target code generator.
- Intermediate code generator produces intermediate code.
- The code optimizer is responsible to optimize the code, and the target code generator generates the machine code / native code.
- In the JIT compiler, there is a special component called profiler.
- Apart interpreter and JIT compiler, there are also several components which are inside execution engines such as Garbage Collector and Security manager.
- Assume it may require native method libraries. There is a Java Native Interface(JNI) which is responsible to provide the native methods. So the execution engine is the central component of JVM.
Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects
Silansoftware Pvt.Ltd, BBSR Call to: 0674-2361252
Really good Explanation....