JVM in details understanding and How it works?

JVM (Java Virtual Machine) Architecture :



As shown in the below architecture diagram, the JVM is divided into three main subsystems:

a. ClassLoader Subsystem
b. Runtime Data Area
c. Execution Engine


1. What is ClassLoader Subsystem:

Java's dynamic class loading functionality is handled by the ClassLoader subsystem. It loads, links. and initializes the class file when it refers to a class for the first time at runtime, not compile time.

1.1 Loading
Classes will be loaded by this component. BootStrap ClassLoader, Extension ClassLoader, and Application ClassLoader are the three ClassLoaders that will help in achieving it.

Lets take an example of different class loaders:
public void ClassLoaders() throws ClassNotFoundException {
    System.out.println("Classloader of this class:"
        + ClassLoader.class.getClassLoader());
    System.out.println("Classloader of Logging:"
        + Logging.class.getClassLoader());
    System.out.println("Classloader of ArrayList:"
        + ArrayList.class.getClassLoader());
}
output is:
Class loader of this class:sun.misc.Launcher$AppClassLoader@18b4aac2
Class loader of Logging:sun.misc.Launcher$ExtClassLoader@3caeaf62
Class loader of ArrayList:null

As we can see, there are three different class loaders here; application, extension, and bootstrap (displayed as null). However, we can see that the last out, for the ArrayList it displays null in the output. This is because the bootstrap class loader is written in native code, not Java – so it doesn't show up as a Java class. Due to this reason, the behavior of the bootstrap class loader will differ across JVMs.

BootStrap ClassLoader
It's mainly responsible for loading JDK internal classes, typically rt.jar and other core libraries located in $JAVA_HOME/jre/lib directory. Additionally, Bootstrap class loader serves as a parent of all the other ClassLoader instances.
This bootstrap class loader is part of the core JVM and is written in native code as pointed out in the above example. Different platforms might have different implementations of this particular class loader.Extension ClassLoader – The extension class loader is a child of the bootstrap class loader and takes care of loading the extensions of the standard core Java classes so that it's available to all applications running on the platform.Responsible for loading classes which are inside the ext folder (jre\lib).
Application ClassLoader –The system or application class loader, on the other hand, takes care of loading all the application level classes into the JVMResponsible for loading Application Level Classpath, path mentioned Environment Variable, etc.
The above ClassLoaders will follow Delegation Hierarchy Algorithm while loading the class files.
What is Delegation Model:
Class loaders follow the delegation model where on request to find a class or resource, a ClassLoader instance will delegate the search of the class or resource to the parent class loader.
Let's say we have a request to load an application class into the JVM. The system class loader first delegates the loading of that class to its parent extension class loader which in turn delegates it to the bootstrap class loader.
Only if the bootstrap and then the extension class loader is unsuccessful in loading the class, the system class loader tries to load the class itself.

1.2 Linking :
   Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get the verification error.
   Prepare – For all static variables memory will be allocated and assigned with default values.
   Resolve – All symbolic memory references are replaced with the original references from Method Area.
1.3 Initialization: This is the final phase of ClassLoading; here, all static variables will be assigned with the original values, and the static block will be executed.

2. What is Runtime Data Area?
The Runtime Data Area is divided into five major components:
Method Area – All the class-level data will be stored here, including static variables. There is only one method area per JVM, and it is a shared resource.
It contains the code actually a compiled code, methods and its data and fields. Runtime constant pool is also a part of Method Area. Memory for it is by default allotted by JVM and can be increased if needed. Runtime Constant pool is per class representation of constant Table. It contains all literals defined at compiled time and references which is going to be solved at runtime.

Heap Area – All the Objects and their corresponding instance variables and arrays will be stored here. There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread-safe.
Heap Memory is created by JVM in start of program and used for storing objects. Heap Memory can be accessed by any thread is further divided into three generations Young Generation,Old & PermGen(Permanent Generation). When object is created then it first go to Young generation(especially Eden space) when objects get old then it moves to Old/tenured Generation. In PermGen space all static & instance variables name-value pairs(name-references for object) are stored. we can manually increase heap size by some JVM parameters as shown

java -Xms=1M -XmX=2M "Class Name"

Stack Area – For every thread, a separate runtime stack will be created. For every method call, one entry will be made in the stack memory which is called Stack Frame. All local variables will be created in the stack memory. The stack area is thread-safe since it is not a shared resource.
The Stack Frame is divided into three subentities:
Local Variable Array – Related to the method how many local variables are involved and the corresponding values will be stored here.
Operand stack – If any intermediate operation is required to perform, operand stack acts as runtime workspace to perform the operation.
Frame data – All symbols corresponding to the method is stored here. In the case of any exception, the catch block information will be maintained in the frame data.

stack area can also be increased by manually:
java -Xss=512M "Class Name"

PC Registers – Each thread will have separate PC Registers, to hold the address of current executing instruction once the instruction is executed the PC register will be updated with the next instruction.
Native Method stacks – Native Method Stack holds native method information. For every thread, a separate native method stack will be created.

3. What is Execution Engine?

The bytecode, which is assigned to the Runtime Data Area, will be executed by the Execution Engine. The Execution Engine reads the bytecode and executes it piece by piece.
Interpreter – The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the interpreter is that when one method is called multiple times, every time a new interpretation is required.
JIT Compiler – The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine will be using the help of the interpreter in converting byte code, but when it finds repeated code it uses the JIT compiler, which compiles the entire bytecode and changes it to native code. This native code will be used directly for repeated method calls, which improve the performance of the system.
Intermediate Code Generator – Produces intermediate code
Code Optimizer – Responsible for optimizing the intermediate code generated above
Target Code Generator – Responsible for Generating Machine Code or Native Code
Profiler – A special component, responsible for finding hotspots, i.e. whether the method is called multiple times or not.
Garbage Collector: Collects and removes unreferenced objects. Garbage Collection can be triggered by calling System.gc(), but the execution is not guaranteed. Garbage collection of the JVM collects the objects that are created.
Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native Libraries required for the Execution Engine.

Native Method Libraries: This is a collection of the Native Libraries, which is required for the Execution Engine.

Comments