Site icon JavaGoal

Java Virtual Machine(JVM) & JVM Architecture

As we know java is a high-level programming language. Our system/machine can understand only machine level language. So, the machine can’t run the program of java directly because it’s written in a high-level language. It needs to be translated into that machine language.
The java compiler translates the source code (High-level language) to byte code (Not a machine level language). Now Java Virtual Machine(JVM) translates the byte code to machine level language. So we will learn Java Virtual Machine (JVM) & JVM Architecture.

Java Virtual Machine (JVM) is a virtual machine it doesn’t physically exist. It resides in the real machine (your computer) and provides a run time environment in which Java bytecode can be executed. It can also run native language programs (Those programs which are written in other languages like C and C++) and compiled to Java bytecode.

Java Virtual Machine(JVM) specialization

By the use of JVM it possible to make java platform independent. Because the compiler has to generate byte code for JVM rather than different machine codes for each type of machine. The compiler translates the code into byte code only it is JVM responsible to convert that code to machine code. The primary function of JVM is to execute the byte code produced by the compiler.

Every operating system has a different JVM, but they produce the same output after the execution of the byte code. It means generated byte code on one operating system can run in different operating systems. The programmer can write code on one system and can run on any other system without any changes. That why Java applications are called WORA (Write Once Run Anywhere).

The compiler compiles .java files and creates .class files that contain the byte codes. Now JVM translates the byte code that will run by an interpreter.

Operation of JVM

classloader – load code: Classloader has three responsibility:

Java Virtual Machine (JVM) Memory

  1. Method/class Area: JVM has only one Method area which is part of JVM memory, and it is a shared resource. The method area contains the class level information of each .class file. Method Area stores per-class structures such as class name, immediate parent class name, methods and variables information, etc. are stored, including static variables.
  2. Heap: JVM has only one heap area which is part of JVM memory. Heap is the run time data area which stores all objects. It is also a shared resource.
  3. Stack: Stack is also part of JVM memory, and it is used for storing temporary variables. It is not a shared resource. Whenever a thread created then JVM creates one run-time stack which is called activation record/stack frame. All local variables of methods stored in their corresponding frame. After completion of the method, the run-time stack will be destroyed by JVM.
  4. PC Registers (Program counter register): Each thread has a separate PC register. The contains which instruction has been executed and which one is going to be executed. 
  5. Native method stack: It stores all the native methods.
  6. Execution Engine: It executes the (bytecode) of the application by use of data and information present in the various memory areas.  It can be classified into three parts: Interpreter: It reads the code line by line and after that, it interprets and executes. Just-In-Time Compiler (JIT): it reads the entire bytecode and changes it to native code. Garbage Collector: It destroys all the unreferenced objects.
  7. Native Method Interface: It provides an interface to communicate with another application written in another language like C, C++, etc
  8. Native Method Libraries: It is a collection of native libraries that is used by the execution engine.

Exit mobile version