JVM : Introduction, Working And Architecture
- JVM stands for java virtual machine.
- Before java virtual machine most of programs used to run on actual machines or computers. It used to cause a lot of problems due to different machine and OS architecture.
- It gives running java programs its own environment, memory and life cycle to manage executing java programs.
- When you run any of your programs using the java command, read our article about JDK to know more, it creates a java process and starts java virtual machine.
- Inside virtual machine bytes codes are loaded, translated and executed.
Working of JVM
Java virtual machine works in 3 steps
1) Class loader : Loads classes
2) Byte code verifier: Verifies bytes of loaded classes
3) Execution Engine: Executes the code
Architecture of java virtual machine
Let’s try to understand architecture and components
1) Class loader
The class loader is responsible for loading bytecodes into JVM and perform initial linking, verification etc.
Three Key tasks of class loader:
1) Loading: It finds and loads binary data for type.
2) Linking: Performs verification, preparation, and (optionally) resolution:
Verification: It ensures correctness of imported type.
Preparation: Allocation of memory and initialization of variables with initial values.
Resolution: Transforms symbolic references to direct references.
3) Initialization: Invokes Java code that initializes class variables to their proper starting values.
There are two types of class loaders
- Bootstrap class loader
- User defined class loader
2) Method Area
Method area contains code and structure of methods.
It stores all java objects.
4) Java virtual machine language Stacks
This stores local variables and each thread has its own stack in java VM.
5) PC Registers
This stores JVM instruction which is currently executing, each thread has its own register.
6) Native Method Stacks
This holds the instruction of native code examples C/C++ etc.
7) Execution Engine
Set of software to verify the complete system.
8) Native Method interface
This allows running java code to invoke native codes/libraries(Example: C/C++).
9) Native Method Libraries
Collection of native libraries (C/C++).
What Are JVM Based Languages
Java is one of the most popular JVM languages to create code which runs on JVM. There are Scala, Kotlin and programming languages as well which run on JVM.
Please note that JVM is heart of Java and any other java virtual machine based programming languages (Scala or Kotlin etc.). If we go into more details this will become a complex topic. Above we have provided a good fundamental detail of JVM. In coming topics we will cover java virtual machine in more detail.