What is Java Virtual Machine?
I hope newbies for the software engineering field have an idea about Java Virtual Machine (JVM). But most of them are failed to dive into JVM. Therefore, I am going to explain clear idea about JVM. First thing we want know what is virtual machine?
Virtual Machine (VM)
Virtual means that is not physically exist. Machine is something that makes our work easier. We can divide VM into two parts.
- System Virtual Machine :- One or some hardware creates multiple independent environments. Eg. Hypervisor, Xen
- Application Virtual Machine (Process VM) :- It creates a platform to run other programs. No hardware involves. It converts inputs into different outputs. Eg. JVM, CLR, PVM
So, if we want to work with JVM, we can’t install JVM, instead we want to install either JRE or JDK. If we a developer we want to install JDK , if you want to just run an application JRE is enough. JRE is platform dependent. When we run the java bytecode(.class file), JRE will deploy the code to create JVM instance. Every java program will create one JVM instance.
JVM will do 3 jobs to convert bytecode into machine code.
- Loading :- Class Loader
- Storing :- Memory area
- Executing :- Execution engine
Now lets look into each part.
Class Loader
There are 3 steps to load the class.
- Loading
- Linking
- Initialization
Eg. Bootstrap class loader, Extension class loader, Application class loader.
Loading
When we load the class into JVM, it checks the following things :
- Fully qualified class name
- Instance variable information
- Immediate parent information
- Whether it is a class or interface or enum.
After this, it creates Class type object (type is Class) and stored in heap.
Linking
There are 3 steps under linking process.
- Verification :- Bytecode verifier will check whether the class is safe to execute. Therefore, it should pass the following conditions i.e correct structure, correct format, valid compiler. Otherwise it will throw runtime exception called verifier exception.
- Preparation :- Assign default values to static and instance variables. Eg. default value for integer is 0, Boolean is false.
- Resolution :- In java program we create objects with its real names. But machine will not understand this. So JVM will replace symbolic link with its direct link i.e replace the object with its memory address.
Initialization
Assign real values(actual value) to static and instance variables. And static block is also executed. Initialization process should be executed before the following 6 active uses.
- Use of a new keyword
- Invoking static method
- Assigning value to static variable
- Invoking reflection API - getInstance()
- Invoking main method (Initial class)
- Instantiating sub class.
There are 4 ways to initialize a class.
- using new keyword
- clone()
- getInstance() - Reflection API
- IO.ObjectInputStream
Memory Area
There are 5 parts in JVM internal memory.
- Method area :- Class information, type information, static and instance variable information will be stored in this area. JVM will create only one method area for a program execution.
- Heap area :- Object references will be stored in this area. All objects data including string and array are also stored.
- Stack :- Method information, local variable information will be stored. Stack frame will be created for each method. When a method is finished, that frame will pop out from stack. So the particular method’s local variables will no longer exist.
- PC register :- Hold next method execution information. i.e it holds order of execution. PC register is created per thread. When a method uses native method, that thread will have null value in pc register.
5. Native method area :- Native method information will be stored. When java does not provide functionality that you want to use, there you have to use native method, that is implemented in other programming languages. (C/C++).
Execution Engine
This is the final job of JVM. It has 3 components.
- Interpreter :- It reads the bytecode and interprets(convert) into machine code(native code) line by line and executes in a sequential manner. If one method is used multiple times, it interprets that method multiple times, this reduces the system performance and makes slow execution. To overcome this problem JIT compiler is used parallelly with interpreter.
- JIT compiler (Just in Time compiler) :- It improves the performance of java program by compiling bytecode into machine code at runtime. It directly calls the compiled code in which places the repeated method is used, this reduces the amount of time needed for compilation. When JVM first starts up, thousands of methods are invoked, thus significantly affects start up time, even if the end result is very good performance optimization.
- Garbage collector :- It is a daemon thread which runs in background, manages the memory automatically. It frees up the heap memory by destroying unreachable methods. i.e removing unreferenced objects, self referenced objects.
We have Java Native Interface(JNI) to provide native libraries to the execution engine. It connects execution engine with native libraries which are written in other programming languages(C/C++) while executing.
JVM is specification and JRE is implementation. And JVM physically does not exist. That’s all about JVM.
Comment out If I missed any points and do let me know your suggestions in the comment section below.