In this article we discuss what is java virtual machine, JVM architecture, How JVM works, the Role of JVM in Java, what are the functions of JVM, and also learn about the Compilation and Execution Process in Java VM.
So let’s start from the beginning.
What is JVM in java?
The full form of JVM is “Java Virtual Machine” and it is an abstract computing machine. yes, it is called a virtual machine. because it does not exist physically. JVM provides a Runtime environment. where java bytecode (.ClassFile) can be executed.
JVM can also run those programs, which are written in other languages and compiled into the Java bytecode. It is available for Hardware and software platforms.
JVM is platform dependent, because the configuration of every OS system is different from each other.
Note:- JVM is the only one Who can call the main method, which is present in the Java code.
There are 3 Notation of JVM.
- Specifications: JVM has Some specification, which provides fully detailed requirements for implementations. its implementation is distributed by the Oracle Company.
- Implementation: JVM is part of JRE (Java Runtime Environment), and its Implementation is known as JRE.
- (Runtime) Instance: when we are put a commend ( java classFileName) in the prompt at that time instance of JVM is created. Which runs our class file or application.
Must Read: What is the Advantages of Java?
In every operating system for each application have one instance which is running into your computer. if multiple applications are running at the same time in your operating system, that means there are multiple JVM instances.
Every single instance manages its own application. that makes sure each application runs in a secure environment.
|Did You Know Who developed JVM?|
|JVM was Designed by Sun Microsystem in the year of 1994, and the current version is JavaSE 15. Now JVM is maintained by the expert programmers of oracle company.|
How does JVM work?
The JVM performs the following some main tasks:
- Loads code: The first step of JVM loads the source code.
- Verifies the code: After then it verifies the code, is there any coding mistake or not.
- Executes code: Then it will execute the code and generate the (.classfile) byteCode.
- Provides the runtime environment: In the final step, JVM passes bytecode (.classFile) to the operating system to perform the operations.
These are the basic functions of JVM.
Internally JVM has 9 major parts. Now let’s understand one by one of JVM Architecture. How it contains the classloader, storage area, native libraries etc.
- Classloader System: Classloader is the subsystem of JVM. The name of the classloader defines its works, it loads the (.classFile) in the System, where we can run the java program. it is loaded by the classloader.
There are 3 activities in the Classloader which are mainly responsible.
- Bootstrap class loader: it is the parent class of Extension Classloader. it loads the (rt.jar) file. in this jar file stored standard edition package classes and all class files such as .net package classes, .lang package classes, java.Util package classes, .sql package class etc.
- Extension class loader: it is the parent class loader of the system classloader and it is also a child classloader of the bootstrap classloader. this classloader loads jar files from the directory which is located on this path $JAVA_HOME/jre/lib/ext.
- Application Or System class loader: It is a child classloader or Extension classloader, and it loads the class files from the file path. You can set a path manually for compilation.
- Method Area of JVM: Basically we also call the methods area as a class area in java. In the JVM method area Stored all the Data including class name, parent class name, methods, variables, and static variables information. Every single JVM has one method area, which has shared resources.
- Heap memory area: it’s stored all the objects and their Data, like name and address of objects. every JVM has a one heap memory area and it is also a shared resource.
- Stack memory area: Stack memory area is used for threads, Every thread has a private stack is called stack frame.
- local variables are always stored in the Stack memory.
- it plays a role as a part of method invocation or method returning..
- It is not a shared resource.
- After getting threads, Jvm destroyed the runtime stake.
- When method invocation is completed, all the frames are destroyed by JVM.
- PC (Program Counter) Register: Program counter register stored the address of currently execution instruction of threads. Every single thread has a separate program counter register.
- Native Method Stacks: it stores Native methods information which is used in the application. after every thread native stack is created.
- Execution Engine: Execution process is done by the execution engine, it executes the bytecode(.classFile). This engine reads the byte code line by line and uses information and data from memory. after then it will execute the programs according to the instructions.
It Contain 3 major parts:
- Interpreter: it is Execute the byte-code line by line and produce the output according to java code instructions. java is interpreted, that’s why it performs independently.
- Garbage collector: JVM manages all program memory with the help of a garbage collector. and it runs inside the JVM. Which continuously identifies unused references and destroys them for memory optimization.
- JIT (Just-In-Time Compiler): In Short it reduces the compilation time for interpreters. JIT compiles the bytecode and changes it into the native code. When the interpreter is noticed the repeated method calls, then it gives direct native code for that part, with this process the interpreter saves time and iteration of code.
- JNI (Java Native Interface): it is a Native interface framework, which helps to communicate with another application that is written in other languages (C++, C) with Native method libraries. and These are the OS libraries, which are mandatorily required for the execution of code.
Java Compilation and Execution Process by Java VM.
Let’s suppose, we create 3 different classes a1,a2, and a3. and the main method is present in the a1.java file.
Now we are creating 2 different methods m1 and m2. where m1 is stored in the a2.jav file and m2 is stored in the a3.java file.
You can observe in the diagram how it’s going to convert high-level code to bytecode.
The compiler will compile all 3 source code which is saved with (.java) extension, and return us a (.class) file. That file contains the byte-code (only machines can understand byte code).
In the machine JVM is present in the reside in the RAM.
During the execution process, the classloader loads the class files in the RAM. After that, the Bytecode verifier verifies the code. an execution engine will convert bytecode into the native code and passed to the operating system to perform tasks.
Admin Words:- thank you for visiting the article. Hopefully, now you can understand what is JVM, and it’s architecture, as well as How JVM works, why JVM is an important part of Java, functions of JVM and java Compilation and Execution Process.
If you have any confusion regarding this article please feel free to ask in the comment box below.
Thank You, Sharing is Caring 🙂