Definition of Java Virtual Machine
JVM is responsible to run Java bytecode by translating them into current OS machine language. Moreover, JVM provides runtime environment with an equally partitioned identical memory area for executing java Bytecode in any OS.
The main reason behind the invention of JVM is to achieve platform independency, means JVM is invented for running java compiled code (bytecode) in all available OS irrespective of the OS in which it is compiled.
Background process : Running java command
We run java command, JVM is created as a process in OS by occupying some memory from RAM and JVM performs internally below three phases:-
- Class Loading phase:
- Once JVM process is create, JVM will request Classloader to load the given class bytecode.
- If class bytecode not available then ClassLoader throw exception ClassNotFoundException / NoClassDefFoundError (both are same), from Java 8 onwards Error: Could not find or load main class.
- ByteCode verification phase:
- After class loading phase, Bytecode verifier verifies the loaded Bytecode’s internal format. if the bytecode is in JVM understandable format, it allows interpreter to execute loaded bytecode.
- If loaded bytecode is not in the JVM understandable format, it’ll terminate execution by throwing exception java.lang.ClassFormatError.
- Execution / Interpretation phase:
- After bytecode verification completed, interpreter will executed loaded bytecode by converting them into current OS machine language.
- While executing bytecode if any logical mistakes found, JVM will throw associated exception.
JVM Architecture block diagram
ClassLoader is a class it is responsible to perform below three activities
- Loading : In class loading phase ClassLoader will load & store given class bytecode into JVM’s Method area. ClassLoader subsystem contains three types of class loaders for loading classes.
- BootstrapClassLoader : It is responsible to load java library classes from BootStrapClasspath. i.e.; from jdk\jre\lib\rt.jar file.
- EtensionClassLoader : It is responsible to load user defined classes or java library classes from ExtensionClasspath, i.e.; from jdk\jre\lib\ext folder.
- This class loader is implement in java with class name ExtClassLoader.
- ApplicationClassLoader/SystemClassLoader : It is responsible to load user defined classes from ApplicationClasspath. It’ll load classes from current working directory or from the folders configured in class path environment variable.
- Linking : Linking is the process of incorporating the binary type data into the runtime state of the virtual machine.
- Verification phase, Classloader will check the type is properly formate and fit for use by JVM.
- Preparation phase, static variables memory is allocate with default values based on their data type.
- Resolution phase, symbolic references in the constant pool are transfer into their direct references.
- Initialization : In initialisation phase, static blocks execute and the static variables are initialize with their initial values give in their declaration.
Runtime Data Areas :
- Method Area :
- In Method area, every class bytecode is load and store. It means the given class static and instance variables declaration statements, blocks, methods, and constructors logic will be store in method area.
- JVM will throw OutOfMemoryError exception if there is not sufficient memory in method area.
- Heap :
- It is like main memory of JVM, all classes and arrays objects are create in heap area. Means given class instance variables memory is allocate in heap area.
- JVM will throw OutOfMemoryError exception if there is not sufficient memory in heap area.
- Java stacks :
- All methods(non-native), blocks and constructors logic is executed.
- we can create several threads for executing methods at a time concurrently. JVM by default creates minimum two threads
- main thread
- garbage collector thread
- When we invoke a method or constructor a new stack frame create. Once this method execution completed this stack frame is destroyed.
- Stack frame holds logic of the method, it’s parameters, local variables, its return values and intermediate calculations.
- Program Counter Registers Area :
- PC Register is create when new thread is create, after thread execution complete it is destroy automatically.
- It is used for tracking execution in this thread by storing its instruction address.
- Native Method Stack Area :
- In Native methods stack area, all native methods are execute.
- Native method is written in some other language, such as C, C++, or assembly, and compiled to the native machine code of a particular processor.
- to create native moths we must place native keyword in its prototype and it should not have body. A java program interact with the host OS by invoking native methods.
Execution Engine :
- It’s responsible to execute java bytecode.
- All executions happens in JVM are control by execution Engine.
- It uses interpreter, JIT compiler, Profiler, Security Manager for executing a program.
- It also contains garbage Collector for destroying unreference objects from Heap Area.
- JIT Compiiler will find the code which is executing repeatedly, generates machine language code only once, buffer it and reuses the same machine language code for repeated execution.
Java Native Interface (JNI) :
- The Java Native Interface (JNI) enables Java code running in a Java virtual machine (JVM) to call and be invoke by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.