JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. |
· Virtual Machine. : In general terms VM is a SW that creates environment between the computer platform and end user in which end user can operate programs
· JVM:- It is also a VM that runs java bytecode by creating five identical runtime areas to execute class members.
This bite code is generated by java compiler in a JVM understandable format.
How can we start JVM process?
· By using “Java” tool
· The java launcher, java, initiates the JVM instance.
·
JVM Architecture Block diagram:
Explanation on runtime areas:
· Whenever we execute a class by specifying its corresponding class name by using the command “java <ClassName>” the java launcher, java, immediately initiates the Java runtime environment for the class execution as a layer on top of OS ,
· and further the entire setup is devided into 5 runtime areas named as:
1. Method Area
2. Heap Area
3. Java Stacks Area
4. Program counter register Area
5. Native Methods Stacks Area
Method area:
· All classes’ bytecode is loaded and stored in this runtime area, and all static variables are created.
Heap area:
· It is the main memory of JVM. All objects of class , non-static variables memory are created in this runtime area.
Java stacks area:
· In this runtime area all java methods are executed.
· In this runtime JVM by default creates 2 threads, they are:
1) main thread
2) garbage collector thread
· Main thread is responsible to execute java methods starts with main method, also responsible to create objects in heap area if it finds “new” keyword in any method logic.
· Garbage collection thread is responsible to destroy all unused objects from heap area.
· For each method execution JVM creates separate block in main thread.Technically this block is called stack frame.
· This stack frame is created when method is called, and is destroyed after method execution
Program counter register area:
· In this run time area a separate program counter register is created for every thread for tracking that thread execution by storing its instruction address.
Native methods stacks area:
· In native methods stacks area all java native methods are executed.
Native method:
· The java method that has logic in c, c++ is called native method.
· To create native method we must place native keyword in its prototype and it should not have body.
Ex:
class example{
public static native int add(int x, int y);
public static void main(String[] args){
add(10,20);
}
}
Java Native Interface:
· It is also called Native method interface, It is a mediator between java native method and original native method for linking its method calls
Execution engine:
· All Executions happening in JVM are controlled by Execution Engine.
ClassLoader Subsystem:
· ClassLoader is a class that is responsible to loading classes into JVM’s method area.
· We have basically 3 types of ClassLoaders
1) ApplicationClassLoader
2) ExtensionClassLoader
3) BootstrapClassLoader
· ApplicationClassLoader is responsible to load classes from Applicaion Classpath(current working directory), it basically uses Classpath environment variable to locate the class’s “.class ” file.
· ExtensionClassLoader is responsible to load classes from Extension Classpath, (“%JAVA_HOME%\jre\lib\ext” folder)
· BootstrapClassLoader is responsible for load classes from BootstrapClasspath, , (“%JAVA_HOME%\jre\lib\rt.jar”) these classes are predefined classes.
ClassLoader working procedure:
· · When JVM come across a type, it checks for that class bytecode in method area.
· If it is already loaded it makes use of that type.
· · If it is not yet loaded, it requests ClassLoader subsystem to load that class’s bytecodes in method area from that class respective classpath.
· · Then ClassLoader subsystem, first handovers this request to ApplicationClassLoader, then this ClassLoader searches for that class in the folders configured in classpath environment variable.
·If class is not found , it forwards this request to ExtensionClassLoader . then it searches that class in ExtensionClasspath.
· ·If class is not found , it forwards this request to BootStrapClassLoader . then it searches that class in BootstrapClasspath.
· · If here also class not found, JVM throws an exception “java.lang.NoClassDefFoundError” or “java.lang.ClassNotFoundException”.
· · If class is found in any one of the classpath, the respective ClassLoader loads that class into JVM’s method area.
· · Then JVM uses that loaded class bytecodes to complete the ececution.
Thread & StackFrame Architecture:
· Thread is a sequential flow of Execution created in JSA.
· StackFrame is a sub block inside a thread, created to execute a method or block, and is destroyed automatically after that completion of method execution.
· If this method has any local variables, they all are created inside that method’s stack frame, and are destroyed automatically when StackFrame is destroyed.
StackFrame architecture:
· Stackframe is internally devided into three blocks to create that method’s local variables, to store instructions, and to execute its logic.
They are:
1) Variable storage area
2) Instructions storage area
3) Method stack
· All local variables are created in Variables storage area.
· All method instructions are stored in Instructions storage area.
· Method logic is executed in method stack.
· To execute method logic , method stack is devided into number of blocks each block is called slot.
What happened in JVM when we execute java command?
· When java command is executed, JVM is created as a layer on top of OS, and is devided in 5 runtime areas as shown above.
· For executing the requested classes, JVM internally performs below three phases.
They are:
1. Classloading: JVM requests classloader to load the class from its respective Classpath. Then class loaded in method area by using java.lang.Class object memory.
2. Bytecode verification phase: After Classloading , BytecodeVerifier verifies the loaded bytecode’s internal format. If those bytecodes are not in jvm understandable format, it terminates the execution process by throwing exception “Java.lang.classFormatError” . if loaded bytecodes are valid, it allows interpreter to execute those bytecodes.
3. Execution / interpretation phase: then interpreter converts bytecodes into current OS understandable format.
Finally, jvm generates output with the help of OS.
0 comments :
Post a Comment