In general we can say JVM is a program which takes byte code(.class file[the compilation of .java file]) and convert 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 3 modules, such as:
Class Loader Subsystem
Class Loader Subsystem:
Class Loader 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 bootstrap class loader, extension class loader, and application class loader.
Bootstrap class loader is responsible for loading class from bootstrap class path(rt.jar).
All the core java API classes are loaded in bootstrap class loader.
Extension class loader is responsible for loading the classes present inside the exe folder.
Application class loader is responsible to load application level class path such as environment variable or environmental class path.
Among these 3 class loaders, bootstrap class loader having highest priority.
If 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 have generated byte code properly or not.
If verification fails, then verify error will generate.
Then prepare for static variable, memory will 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 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 communicate 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 where as the data stored in stack area and PC registers are thread safe.
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 consist of different components, such as intermediate code generator, code optimizer and target code generator.