Knowing and understanding the fundamental concept of anything that you want to learn is very important. As a java developer, you should know about the fundamental concept of java, how it is working and who behind it is. I hope that, by reading this article, you can get some ideas about Java Virtual Machine (JVM)
Before going into the JVM, let’s have a small talk about :
What is java?
Java is the most popular object-oriented programming language, developed by James Gosling at Sun Microsystems in 1995 and is now owned by Oracle. It is used to develop different base applications like desktop applications, web applications, mobile applications, games and more.
What is java Architecture?
As you know, the source code will be compiled into the byte code by the java compiler then the byte code will be converted into the machine code by JVM. Then only the machine code will be executed at any engine. Hence, Java is called write once and run anywhere and platform-independent as well.
The below Image illustrates the functions of java architecture.
What are the three main components in java architecture?
1. Java Development Kit. (JDK)
2. Java Runtime Environment. (JRE)
3. Java Virtual Machine. (JVM)
The below image illustrates the overview of java architecture components.
JDK internally contains JRE, JVM and JRE contain JVM, java class libraries. Indeed Nothing is inside nothing. Each component should be installed in our machine as a package except JVM. let’s have a look at these components by following this article.
What is Java Development Kit(JDK)
JDK is a package of tools for developing Java-based software and it allows to execution of the programs on JRE and JVM.
What is Java Runtime Environment(JRE)
JRE is a package of tools for running Java code.JRE can be used without JDK support, However, JDK requires JRE to create a sustainable environment to run any java application in virtually any operating system.
What is Java Virtual Machine(JVM)
The common job of the JVM is that read the .class file and convert the byte code into the machine code based on the operating system. Hence, Java is platform-independent. But, the JRE and JVM are platform dependant. Because each operating system can have its own configuration. The JRE is installed based on our Operating system and JVM depends on the call from JRE where it was installed.
JVM is a program that is included in the JDK and JRE. Since It is called a Virtual Machine because We can install the JDK and JRE packages but cannot install or uninstall the JVM. Only, when we execute our java application, JRE requests to create a new instance of JVM for the particular application. Otherwise, it does not really exist. And, It destroys when the program is stopped.
When is the JVM instance created actually?
Let see this example.
session\session 1>javac HelloWorld.java
- javac keyword says that compile the HelloWorld java file
session\session 1>java HelloWorld
- here the java keyword says to the operating system that creates JVM instance for this program.
- when you are running four different programs in your machine, how many JVM instances are there? four JVM instances will be created.
- How is the Java platform independent? the JVM read the compiled file and convert it into the machine code based on the operation system. Actually, the JRE creates the JVM based on the OS. Hence, we can run the compiled file in any system like windows, mac,linux.
What are the Components of JVM?
JVM works with three components which are :
1. ClassLoader :- to load the class files
2. Memory Area :- to store the classes
3. Execution Engine :- to execute /provide run time environment
JVM memory area can be divided into five sub-areas and the size of the memory area depend on provided JVM implementation.
Method Area — When loading the class, all information about the class such as class name, immediate parent class name, methods and variables information will be stored in the Method area. One for each JVM
Heap Area — When loading the class, all object and instance variables will be stored in the heap area. one for each JVM
Stack Area — It creates frames for local variables and methods, and at end of each method execution, the particular frame of a method will be destroyed with stored data in this particular frame. It is created per thread.
PC Register — Holds the information about the current execution. It means that contains the address. It is created per thread
Native Method Area — Hols all the native method information of an application. It is created per thread
Classloader means that how classes come to the memory area during runtime dynamically. There are three functions in the class loader
- Loading :- When JVM request the .class file, the classloader loads the class file and the loading mechanism takes
- a fully qualified name of the class.
- it will check the class structure whether is it class or interface or enum
- read immediate parent and details about static variables, modifiers and methods.
The JVM /classloader stores this information in the memory area by JVM and creates an object in Class data type to indicate the information. then, we can use this object for getting the information like the name of the class, parent name, methods and variable information using the getClass() method.
- Linking :- Linking has also three different phases
I. Verification — Java has byte code verification in JVM. It checks whether the class file is safe to execute by checking the class file comes from the correct compiler, with the correct structure and format or not
II. Preparation — In here, assign a default value for instance or static variables.
III. Resolution — in here, replace the object with a specific memory location.
- Initialization :- initialize the variables with real value. JVM has a rule that every class must be initialized before,it becomes active use. Cause for a class becomes as active use are:
· Using new keyword
· Having a static method/invoked static method
· Assigning value for a static field
· Be the initial class (main method class)
· Using any reflection APIs
· Be instantiation of a subclass.
Java class files are executed by the execution engine. It has three components for executing the byte code. These are :
Interpreter :- it reads the byte code and interprets the into the machine code one by one line code. as it interprets the same code every time, it reduces the performance of a system. JIT compiler comes as a solution for it.
Just in Time (JIT)Compile:- It makes the improvement on the performance of the system by compiling the byte code into machine code at once. Then, for execution, the compiled code will be used instead of interpreted again.
Garbage Collector — It is a program for managing memory by destroying an unreachable object from the heap memory area.
Difference between JDK, JRE and JVM - javatpoint
We must understand the differences between JDK, JRE, and JVM before proceeding further to Java. See the brief overview…