How JVM Works – JVM Architecture?

JVM(Java Virtual Machine) acts as a run-time engine to run Java applications.JVM is a part of JRE(Java Run Environment).Java applications are called WORA (Write Once Run Everywhere). This means a programmer can develop Java code on one system and can expect it to run on any other Java enabled system without any adjustment. This is all possible because of JVM.

What exactly is JVM ?

  • A specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies.
  • An implementation Its implementation is known as JRE (Java Runtime Environment).
  • Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is created.
Following is the structure of java code execution .

  

Initially the java source code is compiled by compiler and converted into .class file which contains bytecode. After that .class file .goes through various steps and is converted into machine code.

JVM Internal Acrhitecture  


Internal architecture of JVM contains Classloader, Execution engine and Memory Area. JVM perform following operation while executing its task.

  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment


                                                       JVM Architecture 


ClassLoader : Main responsibility of classloader is loading,linking and intialization. Go to this link to know about classloader.


JVM memory area :




Method area : In method area, all class level information like class name, immediate parent class name, methods and variables information etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource.

Heap area : Information of all objects is stored in heap area. There is also one Heap Area per JVM. It is also a shared resource.

Stack area : For every thread, JVM create one run-time stack which is stored here. Every block of this stack is called activation record/stack frame which store methods calls. All local variables of that method are stored in their corresponding frame. After a thread terminate, it’s run-time stack will be destroyed by JVM. It is not a shared resource.

PC Registers : Store address of current execution instruction of a thread. Obviously each thread has separate PC Registers.

Native method stacks : For every thread, separate native stack is created. It stores native method information.


Execution Engine





Execution engine execute the .class (bytecode). It reads the byte-code line by line, use data and 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.For more on Garbage Collector,refer Garbage Collector.


Java Native Interface (JNI) :It is a interface which interacts with the Native Method Libraries and provides the native libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.

Native Method Libraries : It is a collection of the Native Libraries(C, C++) which are required by the Execution Engine.


This covers the concept of JVM internal architecture . If you have any question or you want to add anything please comment below.

Comments

Popular posts from this blog

Deploy standalone Spring MVC Application in Docker Container

Refactor Code : Separate Query from Modifier

HTTP : Must known Protocol (Part 1)