How Java Virtual Machine JVM works ?


Java Virtual Machine (JVM) is a engine that provides 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
First Java source code converts in   .class file . Which load by the class loader in  JVM JVM   keep   the .class  file in different memory space  allocated . Then execution engine executes the class in machine level code.  where then operating system gives output after processing.

  JVM  Architecture


Class Loader Subsystem

It is mainly responsible for three activities.


Class loader   diagram 

Bootstrap ClassLoader:
The bootstrap class loader loads the core Java libraries located in the /jre/lib directory specifically loads rt.jar
This ClassLoader is written in a native language which is C/C++.
Bootstrap is the parent ClassLoader and Extension ClassLoader falls under it.
Extension ClassLoader:
It loads classes from the JDK Extension located in the, /jre/lib/ext
Extension ClassLoader is implemented by the sun.misc.Launcher$ExtClassLoader class.
System ClassLoader is the child of this ClassLoader.
System ClassLoader:
This loader is responsible for loading the classes found on java classpath.
It is implemented by the sun.misc.Launcher$AppClassLoader class.
Setting the classpath through an environment variable.

set CLASSPATH=D:\myprogram

java org.mypackage.HelloWorld

Linking : Performs verification, preparation, and (optionally) resolution.
Verification : It ensures the correctness of .class file i.e. it check whether this file is properly formatted and generated by valid compiler or not. If verification fails, we get run-time exception java.lang.VerifyError.
Preparation : JVM allocates memory for class variables and initializing the memory to default values.
Resolution : It is the process of replacing symbolic references from the type with direct references. It is done by searching into method area to locate the referenced entity.

Initialization : In this phase, all static variables are assigned with their values defined in 
the code and static block(if any). This is executed from top to bottom in a class and from parent to child in class hierarchy. Initialization sets the actual variable values instead of the default values allotted during class preparation.
  • Initialization of a class consists of two steps:
    1. Initializing the class’s direct superclass (if any), if the direct superclass hasn’t already been initialized.
    2. Executing the class’s class initialization method, if it has one.
  • Initialization of an interface does not require initialization of its super interfaces. Just the interface’s interface initialization method is executed, if it has one.

JVM Memory
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.

How Java Virtual Machine JVM works ?  How   Java  Virtual Machine  JVM    works   ? Reviewed by Mukesh Jha on 11:18 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.