Main Components of JVM
JVM (Java Virtual Machine)consists of 03 main components.
- Class Loader
- Memory Area
- Execution Engine
Class Loader
Class loader is responsible for loading the .class file of a java file into the memory area.
When loaded into memory area it’s creates an object from “class type” (Special data type in Java). Only one object per class could create. (Only in very first time). Class loader also consist of 03 sub parts.
- Loading
- Linking
- Initialization
~Loading
When loading the class file into memory area it’s going to check the following information of that particular file.
- Full qualified name
- Whether it’s a class / interface
- Immediate parent class information
- Instance variable information
~Linking
Linking also have 03 sub steps such as,
- Verification
For verification process JVM has a byte code verifier. It checks,
- If the code has come from a valid complier
- If the code has the correct structure
- If the code has correct formatting
If those conditions are not satisfied JVM will throw an exception called “Verifier Exception”. If everything is done and no exceptions were found then the verification process is completed.
- Preparation
In preparation process, if you have any instance or static variables in your program it assign default values for those variables.(This is a default value not a initial value)
- Resolution
In resolution process, before reach to the machine level JVM replacing those (domain specific words) symbolic link with direct link with the allocated memory address. (Because, JVM cannot understand those domain specific words. Such as, Student , Employee).
~Initialization
In initialization it assign real value to static variables and it will execute your static block. Every class must be initialize before it’s active use.
Ways of active use for Java class,
- Using new keyword
- Invoke a static method — (employee.verify())
- Assign values for static field
- Initial class — Main
- Usage of reflection — getInstance
- Instantiating subclass
Memory Area
Memory area has 05 more sub areas with it,
- Method Area
This consists all the information regarding class names, parent class details, method /static variables.
- Heap Area
This area has all the details regarding objects.
Note : Each and every JVM instance only have one method and heap area
- Stack
This area consists of all the details about methods and local variables.
- PC Register
This will hold the information about next execution.
Note : Stack and PC Register will create per thread.
- Native Method Area
Any native methods that are to be loaded are listed in the native method section.
Execution Engine
Execution engine has 03 sub sections as well.
- Interpreter
- JIT Compiler
- Garbage Collection