Java Virtual Machine (JVM)3 min read

What is JVM?

JVM stands for Java Virtual Machine, which is an abstract machine that provides a run-time environment to run Java programs. It converts Java byte-code into machine language, so not only Java but it also enables the computer to run other programs that are compiled to byte-code.
JVM is a part of the JRE(Java Run Environment). However, the reason for the Java program is executed by the JVM helps to solve the major problems related to web-based programs and also to make it secure.

JVM working process can be explained in the following manner:

  • It first loads byte-code.
  • then verifies the byte-code.
  • Then executes byte-code providing runtime environment.

JVM Architecture:

1. ClassLoader subsystem:

This subsystem is used for loading class files. It performs three major functions :
Loading, Linking, and Initialization.

There are three built-in classloaders in Java.

  • Bootstrap ClassLoader: It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, package classes, java.util package classes.
  • Extension ClassLoader: It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.
  • System ClassLoader: It loads the class files from the classpath. Also known as Application classloader.

2. JVM Memory:

  • Method Area:
    In method area, structures like the run-time constant pool, field and method data, variables information, static variables, etc. are stored.
  • Heap:
    It is a shared resource among all the threads and created during run-time. All the Objects, metadata, and arrays are stored in the heap.
  • JVM language Stacks:
    Java language Stacks stores parameters, local variables and return addresses during method calls. It is not a shared resource. One JVM stack is created simultaneously for each thread that is each thread contains its own JVM stack.
  • PC Registers:
    PC(Program Counter) Register contains/stores the address of JVM(Java Virtual Machine) instruction that is being currently executed. Each thread in Java has its own PC register.
  • Native Method Stacks:
    Every thread has its own separate native stack hold the native method information.

3. Execution Engine:

The execution engine executes the bytecode, reading it line by line.

It contains three parts:

  • Interpreter:
    The work of the interpreter is to read the byte-code line by line and then execute it.
  • Just-In-Time Compiler(JIT):
    It is used to increase the performance of the compiler by taking the block of similar byte-code that is the repeated method calls. Hence reducing the compilation time and making it more efficient.
  • Garbage Collector:
    It is used to destroy the useless objects or objects that are no longer required.

4. Java Native Interface (JNI):

It provides the native libraries required for the execution of a program. It allows the JVM to call other Libraries like C, C++, Assembly, etc., providing the interface to interact with other Native Application.

5. Native Method Libraries:

This is a collection of the Native Libraries(C, C++) which are required by the Execution Engine. These are the “native” codes, that is, the code that is compiled for a specific hardware architecture or operating systems such as X86 or windows.

Java Variables.
Scope Of a Variables in Java.