Java Virtual Machine (JVM)

is a engine that provide runtime environment to drive the Java code or applications. It converts Java bytecode into machines language. JVM is a part of Java Run Environment (JRE). In Other programming languages, the compiler produces machine code for a particular system. However, Java compiler produces code for a Virtual Machine known as Java Virtual Machine.


Here is how JVM works

  1. First, Java code is complied into bytecode. This bytecode gets interpreted on different machines.
  2. Between host system and Java source, Bytecode is an intermediary language.
  3. JVM is responsible for allocating memory space.

JVM Architecture

Components of the JVM are:

  • Class Loaders
  • Run-Time Data Areas (JVM Memory)
  • Execution Engine



  1. ClassLoader
    The class loader is a subsystem used for loading class files. It performs three major functions viz. Loading, Linking, and Initialization.

  2. Method Area
    JVM Method Area stores class structures like metadata, the constant runtime pool, and the code for methods.

  3. Heap
    All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common and shared across multiple threads. See Java Memory.

  4. JVM language Stacks
    Java language Stacks store local variables, and it’s partial results. Each thread has its own JVM stack, created simultaneously (at the same time) as the thread is created. A new frame is created whenever a method is invoked, and it is deleted when method invocation process is complete.

  5. PC register
    PC register store the address of the Java Virtual machine instruction which is currently executing. In Java, each thread has its separate PC register.

  6. Native Method Stacks
    Native method stacks hold the instruction of native code depends on the native library. It is written in another language instead of Java.

  7. Execution Engine
    It is type of software used to test hardware, software, or complete systems. The test execution engine never carries any information about the tested product.

  8. Native Method Interface
    The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to call by libraries and native applications.

  9. Native Method Libraries Native Libraries is a collection of the Native Libraries (C, C++) which are needed by the Execution Engine.

Class Loader

Run-Time Data Areas

Execution Engine

Execution engine execute the .class (bytecode). It reads the byte-code by line, use data nad information present in various memory area and execute instructions. It can be classified in three parts:

  • Interpreter: It interprets the bytecode line by line and then executes. The disadvantage here is that when one method is called multiple times, every time interpretation is required.
  • Just-In-Time Compiler (JIT): It is used to increase efficiency of interpreter. It compiles the entire bytecode and changes it to native code so whenever interpreter see repeated method calls, JIT provide direct native code for that part so re-interpretation is not required, thus efficiency is improved.
  • Garbage collector: It destroy un-referenced objects.

Memory exception and error

StackOverFlowError

OutOfMemoryError

Memory leak

Reference