Java Virtual Machine (JVM) is responsible for executing the java program line by line hence it is also known as interpreter. The JVM is responsible for interpreting Java bytecode (.class file) and translating this into actions (into machine readable form) or operating system calls.
Every Java developer knows that bytecode will be executed by JRE, (to know more on JRE) but fact is that JRE is the implementation of JVM which analyzes the bytecode, interprets the code and executes it.
As shown in the above architecture diagram JVM is divided into three main subsystems.
Class loader subsystem
Runtime Data Area
- Method Area
- Heap Area
- Stack Area
- PC Registers
- Native Method stacks
- JIT Compiler
- Garbage Collector
In JVM there is a module called Class loader which perform following activities,
- Loads .class file into memory.
- Checks all byte code instructions are proper or not. If it finds issue, execution will be rejected.
- Initialization of static variables.
Classes will be loaded by following components,
BootStrap, Extension and Application/System ClassLoader
- BootStrap ClassLoader: Responsible for loading classes from the bootstrap classpath, nothing but rt.jar. Highest priority will be given to this loader. This file always resides inside lib directory of JRE in Windows and Linux.
- Extension ClassLoader: Responsible for loading classes which are inside ext folder (jre\lib)
- Application/system ClassLoader: Responsible for loading Application Level Classpath, path mentioned Environment Variable etc.
- Verify: Byte code verifier will verify whether the generated byte code is proper or not if verification fails we will get verification error.
- Prepare: For all static variables memory will be allocated and assigned with default values.
- Resolve: All symbolic memory references are replaced with the original references from Method Area.
All static variables will be assigned with the original values and static block will be executed
Runtime Data Area
If the byte instructions are proper, it allocates necessary memory for execution. These memories are divided into 5 major parts are called run time data areas.
- Method Area: This area is used to store all the Class level data will be stored here including static variables.
- Heap Area: In this area where objects are created. Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. All the Objects and its corresponding instance variables and arrays will be stored here.
- Stack Area: All local variables will be created in the stack memory. Java stacks are area where java methods are executed.
- PC (Program counter) registers: This area contains the memory address of the instruction of the methods. It used to hold address of current executing instruction once the instruction is executed the PC register will be updated with the next instruction.
- Native Method stacks: Native methods are executed on native method stacks. To execute native method, native methods libraries are required. These header files are located and connected to JVM by native method interface.
Execution engine contains interpreter and JIT (Just in Time) compiler, which are responsible to converts byte code instruction into machine code so the process will execute them.
- Interpreter: Reads the bytecode, interprets it and executes it one by one. The interpreter interprets the bytecode faster but executes slowly.
- JIT Compiler: I enhance the speed of the execution.
- Garbage Collector: Garbage collector is automatically invoked when the program is being run. It can also be called by calling gc() method of Runtime class or system class in java.
Java Native Interface
It will be connect with the Native Method Libraries and provides the Native Libraries needs for the Execution Engine.
Native Method Libraries
It is the collection of Native Libraries required for Execution Engine.
Top 5 interview questions:
- Explain briefly on JVM architecture.
- What is the role of Class loader in JVM?
- How memories are allocated during program execution?
- How we can call Garbage Collector explicitly?
- What is the use of JIT compiler in java?
Hey guys, Now it is your time! Drop a comment if more details needed or if any update requires. Your comments are more valuable to improve our site to help others.