Java Virtual Machine(JVM) — Part 01

Manupriya Logus
6 min readDec 15, 2020

--

Souce:https://www.signifytechnology.com/blog/2018/04/profiling-jvm-applications-by-eugene-yokota

Most of the developers who are coming to the Java development world already know that there is something called JVM, but what they don’t know is where this JVM exists, what it does, what it can do and so. If we ask what the job of the JVM is, everyone will tell us JVM is converting byte code into machine code or otherwise running a Java program. That’s not wrong but that’s rarely true. Because that is not the complete job of the JVM.

Before getting into JVM you will have to understand about a virtual machine. without understanding a virtual machine there is no way for you to understand JVM. So what is a virtual machine? Please read my previous article on virtual machines.

As you know JVM is a process-based virtual machine. It is a complete specification. When you download the Java Runtime Environment (JRE) JVM comes along with the JRE. When you install JRE, it will deploy all the codes which can create a JVM. so if you are installing JRE on a Windows machine it will deploy the code which is required to create a JVM for the Windows environment. If you install JVM on a Linux environment it will deploy the code which is required to create a JVM for the Linux environment.

If you get a high-level picture for JVM. You should be able to understand that JVM is inside the JRE and JRE is inside the JDK. In JDK there are other tools and components other than JRE, for example, Java C. In JRE there are lots of libraries other than the JVM, so this is a very high-level Picture.

As you may have heard Java has a concept called ‘write once run anywhere’. You can’t blindly accept that because the program which is written to run on the server or a different machine may not exactly run the same way without any modification on a mobile device. So we have limitations and conditions but technically it is yes. Even though Java is platform-independent, JRE is tightly platform dependent. When you install JRE it will deploy the code to create a JVM. So when you execute a Java program it creates a JVM instance and its JVM will take care of converting your Java code to bytecode into machine code. That’s direct terminology.

Let’s see how JVM really does this.

So the moment you start a Java program it will create a JVM instance on your computer and the moment your program exits JVM instance also will die. So a JVM instance will only exist on your computer until you run your java program. So if you’re executing three different Java programs on your computer at the same time then there will be three different JVM instances. Let’s say after five-seconds one program ends, then only you have n — 1 JVMs. Otherwise, if there are three now there are two different JVM instances. When all programs exit then no JVM instance exists on your computer.

Let’s create and compile a simple hello world program. So when the moment you give the Java keyword and space your file name in the Java means you tell your operating system ‘hey give me a JVM instance’. When JVM starts it creates a none demand thread and your class must have a method called public static void main. Let’s discuss why it is in a different article. So when your JVM instance creates, what it does is, it takes this method to execute. You can drive your program through this method. So now we have a daemon thread so this daemon thread is capable of creating the other non-daemon threads. There are two ways this program gets exit otherwise this JVM gets destroyed.

  1. If there are no non-daemon threads that exist. That means all the non-daemon threads which you created are closed or destroyed.
  2. The application can do suicide. So that means it can call System.exit() method then also the JVM will die and exit

so now we have a background idea of who this guy is. That means our JVM exits while the application is running.in other words, JVM exists while at least one non-daemon thread exists. If there is no non-daemon thread then JVM dies. This is the life cycle of JVM. JVM born when your application runs, JVM dies when your application ends.

Let’s open up JVM and look inside. Before doing that, I will explain with a real-world scenario. Let’s assume you have a college activity where you need to throw balls at a target. You have a thousand balls, you get a hundred balls per time and you need to throw those balls. Assume you have three stages: you have a storage which is a big storage with the balls. So you need to take those balls and take them to your place. That is the second stage. In the third stage, you need to take these balls and throw them. So now what are the three activities you need to perform? You have to go and take the balls to your location so you are loading balls from storage to your basket. so then balls can’t stay on the ground you have to have a container like a basket otherwise it will go all over the place. So you load and you store in your basket. Now Your objective is throwing these balls at your target. Now you need to take them through loading, storing, and execution. so these are the three tasks if you want to throw the balls.

Let’s come back to JVM.

class Simple{  
public static void main(String args[]){
System.out.println("Hello Java");
}
}

So you have Java files (Simple.java) and you convert those into class files(Simple.class). Now you need to load these class files into your JVM. Which in other words ClassLoader. So now these loaded classes should be stored. This storing place is the Memory Area. Now you have objects, instructions on your memory someone has to be there to execute which is called Execution Engine.ClassLoader, Memory Area and Execution Engine are the three components inside JVM.

JVM high-level architecture

JVM Architecture

As I mentioned before JVM is only a specification. Its implementation is will vary according to the vendor. Let’s understand the architecture of JVM as defined in the specification.

JVM Architecture
Souce:https://www.facebook.com/cforcoding/photos/a.2531993550185089/2259339930783787

Let’s dig one by one inside into those particular areas through the next article series.

References

[1] https://docs.oracle.com/javase/specs/jvms/se8/html/

[2] https://www.youtube.com/krish

--

--