Are you curious to know what’s inside JVM???😵

Roshni Silva
5 min readNov 30, 2021

From the last story I walked you guys through the basics of JVM but today I would like to take you guys inside to JVM.

So, guys are you ready to go inside JVM??

This storyline started with ‘once upon a time, someone in the world succussed to open up this JVM, which cannot be found in the reality but he could able to find three main components inside this JVM’. Now let’s talk a bit more about the main components. Below is the simple scenario which tells about 3 tasks involved (load, store and execute) with main 3 components in a very simple manner.

Now we have ClassLoader, Memory Area and Execution Engine. Here onwards we’ll look into each and every component separately so let’s start off with ClassLoader.

ClassLoader

ClassLoader is there to load .class files (btye code) into JVM and this ClassLoader is mainly responsible for ‘LOADING’, ‘LINKING’ and ‘INITIALIZATION’.

1 LOADING — The main job of loading is taking .class files and put into JVM. So, when doing this mechanism JVM reads below information

· The fully qualified name of your loaded class

· Information about variables

· Immediate parent of your loaded class

· Whether your loaded class is a class, an interface or an enum

Now let’s go with a simple scenario.

Assume now you have Student.java class and complier convert this into .class file. Now you have Student.class file. Now ClassLoader is there to load your file into JVM and now it reads all the above-mentioned information then JVM creates an object from class type (Guys please don’t confuse. Here I’m not talking about your class which is ‘Student’ but there is a special type in java called ‘Class’) and it assigns this student into that class type object and put into the heap.

****Note — At the very first time only JVM creates an object from the class type for each loaded java class and stores it into the heap.

Okay, now you might think ‘what this is the heap?’. I’ll explain to you guys what is heap please be read forward.

2 LINKING — This can be further divided into 3 parts.

3 INITIALIZATION — In this phase mainly focuses on assigning original values back to the variable as mentioned in the java code and executes static block or static method. Most importantly, JVM enforces the initialization must do before a class becomes an active use. Now let’s have a look at what are the active use cases of a class.

Guys, let’s keep a full stop to ClassLoader and move further.

Memory Area

Soooo….. here we come to the Memory Area component and let’s talk about it.

According to the above diagram, the memory area has 5 different sub-areas in it. Now let me talk to you about each and every sub-area with a brief discussion.

1 METHOD AREA- Loads class data into here and this holds information about class data such as static variables, blocks and static methods and immediate parent class name (if any) and etc.

2 HEAP AREA- when loading classes this is the place all objects are stored.

Note –There is only one method area and a heap per JVM.

3 STACK- In a simple term, this place keeps information of methods, local variables. Stack uses the Last-In-First-Out approach. A block of stack area we called as ‘frame’ and holds local variable information of a method call. When we call a particular method then it will create a frame and once the method execution is over it will remove a particular frame.

4 PC REGISTERS- This holds information about the next execution (if not a native method) and each thread holds its own PC register.

5 NATIVE METHOD AREA- This holds the information about the native methods which are written in language other languages such as C or C++.

Are you ready to go through the final component of JVM??😎

Execution Engine

Execution of the bytecode happens here and it executes the bytecode line-by-line and the execution engine states the priority which class to be executed first. When we talked about sub-components of the execution engine, it has an Interpreter, JIT (Just In Time) Compiler and finally Garbage Collector.

1 INTERPRETER– This reads bytecode then interprets them and executes line by line. Due to that this becomes slow. The downside of this interpreter is that whenever the same method is invoked multiple times, each and every time a new interpretation is needed which caused a reduction in the performance of the system. So, this is where the JIT compiler takes place.

2 JIT COMPILER- In order to overcome the downside of an interpreter, JIT compiler runs parallel to the interpreter. It uses to identify repeated code in the program while the interpreter executes the bytecode line-by-line. JIT compiler compiles bytecode into machine code and this machine code is stored in the cache. Later these codes are used whenever the code is repeated. So, this literally leads to increase performance in java applications.

3 GARBAGE COLLECTOR- The name itself tells it is there to collect garbage or unused objects. This checks the heap area and finds out are there any unreferenced objects. If so this will destroy those objects from the heap and clean the memory. The garbage collector follows Mark and Sweep steps when he is doing his job.

· Mark — In this step garbage collector identifies the unused objects in the heap.

· Sweep — In this step garbage collector removes identified unwanted objects from the heap area.

Guy… I hope you have got some knowledge from this story regarding the inside view of JVM. Now it’s time to come out from JVM and take note of what you have learned.👻

--

--