As we know from earlier posts JRE is the implementation of Java Virtual Machine (JVM). So it is important to know the JVM architecture. In this post we will see the more details about JVM and its architecture.

What is JVM?

The Java Virtual Machine is heart of the Java architecture and it supports all the three features of the java. They are:

  • Platform Independence
  • Network Mobility
  • Security

When a java application starts, a runtime instance is born and when the application completes, the instance dies.Every Java application runs inside in its own Java Virtual Machine. Java Virtual Machine main work is to load class files and execute the bytecode.

JVM consists of class loader where it is used to load the class files from both the program and the Java API, and only those class files are loaded form the Java API which are required by a running program. The bytecodes are executed in an execution engine.

Given below is the figure of java virtual machine architecture.

Now we will understand this architecture in detail.

  • Class Loader: The class loader work is to load all the files which are required to execute the program. The class loader takes the .class file as the input to load the java bytecode. There are three types of java class loader built in java:
    • Bootstrap class loader – The bootstrap class loader will load the JDK internal classes i.e. rt.jar and other classes like java.lang.* package.
    • Extensions class loader – The extension class loader will load the classes from the JDK extension directory such as $JAVA_HOME/lib/ext directory.
    • System class loader – The system class loader will load the classes from the current classpath which can be set while invoking a program by using –cp or –classpath in command line.
  • Runtime Data Areas: In Java Virtual Machine there are various runtime data areas defined which are used during the execution of a program. Some of the data areas are created when the Java Virtual Machine starts and they get destroyed when the Java Virtual Machine exits. Some of the runtime data areas are:
    • Method Area: The method area is shared among all Java Virtual Machine threads. If the allocated memory area is not sufficient during the run time then the JVM will throw the OutOfMemoryError.
    • Heap: The heap is shared among all the Java Virtual Machine threads. It is a run time data area from which memory for all the class instances and array is allocated. Heap is a finite memory and can be configured at the time of setting up of runtime environment using non standard option.
    • Java Stacks: In the java virtual machine the every thread has a private Java Virtual Machine stack which is created at the time of thread is created. It stores frames. It is used to store the information of the methods and it is done at the execution time.
    • PC Registers: The PC Registers stands for program counter registers which contain the address of the next instruction that has to be executed. In the JVM every thread has its own program counter registers.
    • Native Method Stacks: The JVM supports the native method stacks which is used for native methods and is created per thread. If the native methods cannot be loaded by the JVM then it does not have the native method stacks.
  • Execution Engine: The execution engine is used to execute the bytecode which is assigned to the runtime data areas in the JVM via class loader. It reads the bytecode in the form of unit of instruction. The execution engine consists of Interpreter and JIT compiler which are used to convert the bytecode into machine code.
    • Interpreter: The interpreter read the bytecode stream and then executes the instructions one by one.
    • JIT compiler: The JIT stands for Just in Time compiler which is used to improve the performance. It compiles the bytecode and converts it to the native code.
  • Native Method Interface: It is a program which connects the native methods libraries with JVM for executing native methods like C header files.
  • Native Method Library: It holds the native libraries information required for the Execution Engine..