Understanding The Core Concepts Behind JVM, JRE and JDK in Java.

Oshan Vikasith
9 min readFeb 6, 2022

--

Photo by Fotis Fotopoulos on Unsplash

What is Java? Do you know the concepts behind JVM, JRE & JDK !

Java is a High level, object oriented (class based) programming language which was initially invented by James Gosling at Sun Microsystems. Previous java implementations were owned by Sun Microsystems and currently it is owned by Oracle.

Java uses both interpretation and compilation techniques when executing a program. First the java code will be compiled into bytecode. A “.class” file will be generated through this process. Then JVM will go through the interpretation process. Finally the output relevant to the java program will be available.

Now we are going to discuss some of the basic fundamentals which a developer should know about java.

Java comes with three main components, namely

  1. JDK — Java Development Kit

2. JRE — Java Runtime Environment

3. JVM — Java Virtual Machine

If you are going to develop a java program it is essential that you should know what these components do.

In order to create a java program you can use any java enabled IDE/Notepad and a platform which consists of java.

What is JDK (Java Development Kit)?

JDK is the software development environment which is used to develop a java program. It consists of the Java runtime environment, libraries, compiler and the interpreter in order to develop a java based program.

What is JRE (Java Runtime Environment)?

JRE provides implementations regarding java specifications. JRE provides the environment to run a java program (not for development purposes).

JRE is operating system dependent, which means for a windows machine we have to use JRE compatible for a windows environment. Similarly for mac environments have to have a JRE compatible for Mac.

Now we are going to discuss deeply about Java Virtual Machine. In order to discuss JVM you should know about virtual machines.

Java Virtual Machine (JVM)

Virtual Machine

Virtual” means something that does not exist physically, but it gives the experience, just as the real entity with the help of a software or through an artificial influence.

“Virtual Machine” means something that does not exist physically but it gives the experience as if it is real and helps to get the relevant tasks done efficiently. That’s the general Idea of “Virtual Machine”.

There are two types of Virtual Machines in the field of computing.

  • System Based Virtual Machine
  • Application Based Virtual Machine
Figure 1: Types of Virtual Machines

What is JVM (Java Virtual Machine)?

JVM is a process or a program which is not a physical entity; that holds specifications which is relevant to java.

You cannot install or download JVM because it is not a physical entity. JVM comes with the JRE.

Is the JVM platform independent ?

JVM is highly platform dependent, which means the answer for the above question is “No”. The reason behind this is that when we install JRE for windows operating system, JRE will execute code necessary to create JVM instance for windows operating system. If we install JRE for the Mac environment then we will end up with a JVM instance authenticated for the Mac environment. So it is dependable with the operating system.

Similarly JRE and JDK are also platform dependent but the language Java is not platform dependent.

Creation and destruction of JVM instance

Creation of JVM Instance

When we create a java program first what we do is save the code and compile using javac command. If there are no errors in the program then we have the ability to move to the next command, which is by giving java and the name of the java file in the command line environment.

If the Name of the file is Application.java

In order to compile — javac Application.java

Run java program — java Application

The moment we call “java Application” in the command line, what happens is, a JVM instance will be created. Simply calling java in the command line invokes a JVM instance. Then the program will run on that JVM instance.

In the same environment if we implement another java program, then another JVM instance will be created. So there will be two JVM instances created in the system. If we implement multiple java programs then java will facilitate the creation of multiple JVM instances which is equal to the number of programs executed. Each program will run on a separate JVM instance; not all programs run on the same JVM instance.

Eg-: Run 10 java programs, 10 JVM instances will be created.

The JVM instance will exist only till the program runs. If the program terminates then the relevant JVM instance will also get destroyed.

Destruction of JVM instance

Now we are going to discuss how a JVM instance gets destroyed. There are 2 methods

  1. If all non daemon threads are destroyed (JVM instance will exist only till single non daemon thread exists)
  2. Calling System.Exit() method.

Given above are the two ways that a JVM instance can be destroyed.

Will discuss more about daemon and non daemon threads in a future article.

JVM Architecture

There are 3 main components exist in JVM, they are as follows

  1. Class Loader
  2. Memory Area
  3. Execution Engine
Figure 2: JVM Architecture (https://www.freecodecamp.org/news/content/images/size/w1000/2021/01/image-39.png)

Class Loader

Main responsibility of the class loader is to load the class information into the method area of the memory area.

Class Loader is responsible to execute three phases, which are

  1. Loading
  2. Linking
  3. Initialization

Loading

Processes which undertake in loading phase is as follows.

  1. Capture relevant information and store them in the memory(method area)

In the loading phase what happens is initially it reads the “.class” file and generates relevant binary data then stores it in the method area. Below mentioned details will be stored in the memory(method area).

  • Fully qualified name of the loaded class and it’s immediate parent class.
  • Immediate parent class information.
  • Whether it’s a class, interface or enum.
  • Instance variable information.

2. Create a class type object and assign it with the loaded class

After reading the above information JVM will create an object from the class type (class pre-defined in java.lang package) and assign it with the loaded class. Finally this object will be stored into heap memory.

This class type object will be created only once for a loaded class.

Eg-: If there is an Employee class, In the initial stage class loader will load the class and it will read the above-mentioned details and store data in the method area. Then an object from the class type will be created and it will get assigned with the loaded class. Finally the created object will be stored in the heap memory.

Linking

In the Linking process there are 3 phases as follows

  1. Verification
  2. Preparation
  3. Resolution

Verification

In the verification process it will check whether the .class file is safe to execute or not. Ensures the correctness of the .class file.

Java has a component created especially to ensure the correctness of the “.class” file which is the ByteCodeVerifier.

ByteCodeVerifier will check whether the file is properly formatted and whether the file is generated from a valid compiler.

If any issues are identified then a run time exception will be thrown. (java.lang.VerifyError)

This verifying process will be the key activity undergoing in the verification phase.

Preparation

Default values will be assigned to any instance variables and static variables declared in the class through the preparation phase.

Resolution

In the resolution phase symbolic references will be replaced by the direct links.

Initialization

In the initialization phase all the static variables declared in the class and static block(if any) will get assigned with their values.

This process is executed from top to bottom in a class and from parent to child in class hierarchy.

JVM has a limitation that each class should be initialized before it’s active use.

How is class considered to be in active use ?

  1. New keyword used.
  2. Invoke static method.
  3. Assign values to a static field.
  4. If the initial class is the one which consists of the main method.
  5. Use of reflection API (getInstance()).
  6. Instance of a subclass.

If the class is subjected to any of the above points then it is mandatory to go through the initialization process otherwise it is not required.

In java there are multiple ways to initialize a class, below are the ways in which we can use to do the same.

  1. Using the new Keyword.
  2. Using the Clone method. (clone())
  3. Using reflection API. (getInstance())
  4. Using IO.ObjectInputStream Class

Now we covered the functionalities of the class loader and how the class loader performs its actions. When it comes to class loaders there are types which we are not going to discuss further through this article. Here I have mentioned the types of class loaders for you.

  1. Bootstrap Class Loader
  2. Extension Class Loader
  3. System/Application Class Loader

Now we are going to discuss about the second core component of the JVM which is the memory area.

Memory Area

Memory area is included with 5 components, they are as follows

  1. Method Area
  2. Heap area
  3. Stack area
  4. PC Registers
  5. Native method area

Architectural View of Memory Area

Figure 3: Architecture of Memory Area

Now we are going to discuss the functionalities of these components.

Method Area

Class level information will be stored in the method area such as class name, immediate parent class name, method and variable information including static variables.

Heap Area

Information regarding to all the objects will be stored inside the heap area.

Stack Area

Run time stack created for every thread will be stored in the stack.

Every block of this stack is named as activation record/ stack frame which will store the method calls.

All the local variables of that method will be stored in the corresponding stack frame.

Responsible for storing method information.

Run time stack will be destroyed after the termination of the corresponding thread.

PC Registers

If a non native method was executed then pc registers will store the information about the next execution. When a native method gets executed the pc registers will not store any information.

Native method is a java method which is implemented through a different programming language such as C++.

Native method Area

Stores native method information.

After discussing the functionalities of the memory area, it’s better to identify core data types which are used commonly in JVM.

There are two types of data types used in the JVM, namely.

  1. Primitive data type.
  2. Referenced data type.

Primitive data type — Hold the value in the variable itself.

Referenced data type — Hold the reference to the value.

Here in this diagram shows the flow of data types which are used in the JVM.

Figure 4: JVM Data Types

Here we are going to discuss the final component of the JVM which is the execution engine.

Execution Engine

Execution engine is the one which will dedicate its time to execute bytecode.

“.class” file will be read line by line and the data which are stored in different parts of the memory area will be used in the execution process.

Execution engine also consist with three major components, namely

  1. Interpreter
  2. Just-In-Time Compiler (JIT)
  3. Garbage Collector

Interpreter

Interpreter will read the byte code (line by line) and it will be converted into the machine code.

If a single method gets called multiple times then, it is required to go through the interpretation process each time.

Time taken to complete the execution process of an entire code will take some time.

These issue were resolved by the Just-In-Time-Compiler.

Just-In-Time-Compiler (JIT)

Main purpose of this component is to improve the efficiency within the interpreter.

Re-interpretation is not required when a certain method gets called multiple times.

Garbage Collector

Garbage collector will destroy unreferenced/unused objects in the memory. This is an automated mechanism.

In Languages such as C and C++ this process is done through inbuilt methods, through a manual calling process but in Java this process is automated.

This mechanism provides many advantages; through this process it doesn’t let unused objects be stored in the memory for a long time.

Crashing of the development environment due to memory shortage will be managed by destroying unused objects.

Memory efficient and automated are the core benefits of garbage collectors, the process of ensuring memory efficiency is complex, which will be discussed in a future article.

Hope you got a clear and better understanding regarding the core fundamental aspects of java. Hope to see you again in a future article, Thank you for reading !

Here I have included the sources which I have referred.

What is JVM — Java Virtual Machine — YouTube

How JVM Works — JVM Architecture? — GeeksforGeeks

JVM Tutorial — Java Virtual Machine Architecture Explained for Beginners (freecodecamp.org)

Garbage Collection in Java — GeeksforGeeks

--

--

No responses yet