Java Virtual Machine (JVM)

Facebooktwitterredditpinterestlinkedinmailby feather

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode.

It controls the execution of the Java programs and enables features such as automated exception handling, Garbage Collection.  When we compile any Java file, the file got translated into the .class file that contains the bytecodes. That “.class” file dives into the number of steps when we execute it. The whole process together describes the JVM.

Java Virtual Machine

ClassLoader

It handles below three activities

  • Loading: It reads the .class file and generates the equivalent binary data and stores it in the method area. For every .class file, it saves fully qualified names of the loaded class, its immediate parent class, modifier, variables, and method information in the method area. JVM creates an object of type Class in the heap memory after loading the .class file.
  • Linking: It checks whether the .class file is formatted correctly and generated by a valid compiler or not. It returns a run-time exception if verification fails. JVM allocates memory for class variables and initializes the memory to default value.
  • Initialization: JVM assigns values to all the static variables defined in the code. It follows the top to bottom approach, i.e. the values assigned from top to bottom or parent to child in the class hierarchy.

Below are three built-in class loaders in Java:

  1. Bootstrap ClassLoader: It is the superclass of the Extension classloader, which loads the rt.jar file that contains all the class files of Java Standard Edition packages such as java.lang, java.net, java.util etc..
  2. Extension ClassLoader: It is the child classloader of Bootstrap and parent classloader that loads the jar files located inside $JAVA_HOME/jre/lib/ext directory or any other directory specified by the java.ext.dirs system property.
  3. System/ Application ClassLoader:It is the child classloader of the Extension classloader. It loads classes from application classpath and internally uses Environment Variable which is associated with java.class.path.

JVM Memory

The JVM memory area divided into these following area:

Method area: It consists of all class level information such as class name, immediate parent class name, variables including static variable and methods details, etc.

Heap area: The heap area is a shared resource that stores the information of all objects at the runtime.

Stack area: For each thread, there is a private JVM stack created at the same time as thread created. It stores frames and holds local variables and play a role in method invocation and return. After the termination of the thread, its runtime stack will be destroyed.

PC Registers: Each thread has its separate PC register and stores the address of the instruction being executed currently.

Native method stacks: Contains all the native methods used in the application.

Execution Engine

The execution engine executes the .class file and reads the byte-code line by line. It contains:

  • A virtual processor.
  • An interpreter that translates the bytecode line by line and executes.
  • Just-In-Time (JIT) compiler: It compiles the bytecode and changes it into native code.

Java Native Interface (JNI)

JNI interacts with the Native Method Libraries and makes available its libraries required for the execution and communicates to libraries written in another language like C/C++.

Native Method Libraries

A collection of the libraries that are written in another language such as C/C++ that are required by the Execution Engine.

Facebooktwitterredditpinterestlinkedinmailby feather