JAVA VIRTUAL MACHINE! 🙄

Irushinie Muthunayake
Nerd For Tech
Published in
7 min readMay 3, 2021

--

Let's understand the Concept of Virtual.

Photo illustration: Man engage in Virtual Games

Virtual → The meaning of “Virtual” is Not in Reality. It means that it doesn't physically existing.

Then what is “Virtual Machine”?🙄

Machine → A machine is a device which helps to do our tasks.

So, The “VIRTUAL MACHINE” is that There’s a machine but that's not real.

Photo illustration: Two types of Virtual Machines

Lets move to “Java Virtual Machine”

✦ Java Virtual Machine (JVM) is completely a specification. It specifies how things should be done.

✦ When downloading JRE, JVM comes with JRE.

✦ When installing JRE, it will deploy all the codes which can create JVM. And JRE is tightly platform-dependent.

✦ When we are executing a java program it creates a JVM instance and then JVM will convert the byte code to machine code.

✦ The time where there is no program is executing, at that particular time, there is no JVM instance running in the computer. (Only JDK and JRE is there)

✦ At the moment we start a java program → Then it will create a JVM instance in the computer.

✦ If we are not executing any program in the computer at that particular time frame, we don't have a JVM instance in the Computer, only JDK and JRE are there.

✦ When we start a java program -> then it will create a JVM instance on the computer.

Photo illustration: How a java program convert to a machine code

Threads (thread allow a program to operate more efficiently by doing multiple things at the same time)

🔵 Daemon Threads: It's a low-priority thread and it performs the tasks like garbage collection. This daemon thread is for supporting tasks.

🔵 Non — Daemon Threads: It’s like the main thread. When JVM starts -> It creates the non-daemon thread.

At what time does this JVM die :

🟢 When there's no non-daemon thread exists.

🟢 An application can do suicide, So it can call the System exit method.

Components in JVM

  1. ) ClassLoader — Load
  2. ) Memory Area — Store
  3. ) Execution Engine — Execute
Photo illustration: Architecture of JDK, JRE, and JVM
Photo illustration: JVM Architecture (Reference: https://javatutorial.net/jvm-explained)

Memory Area

Photo illustration: Structure of JVM

🟨 Method Area-This will keep class information

🟨 Heap Area -This will hold Object Information

🟨 Stack Area-This will hold Local Variables and method information

🟨 PC Registry- This is supposed to hold information about java methods but not the native methods. It holds information about the next execution if it's not a native method

🟨 Native Method Area- Provide facilities to hold info native methods, method information, and other stuff

Photo illustration: Connection in between Stack, PC Register, and Native method area

Classloader

There are 2 types of classloaders. They are Bootstrap classloader and Custom-defined classloader.

Classloader has 3 phases, Loading, Linking, and Initialization.

🔹 Loading: Before loading the class,

〰️It reads the fully qualified class name.

〰️It reads immediate parent information.

〰️It reads whether this is a class, an interface, or an enum. (enum represents a group of constants)

〰️It reads variable information.

Then it loads the class file to the memory area. When it is loaded, JVM creates an Object from class type.

🔹 Linking: This is again divided into 3.

  1. Verification: It checks whether this class file is safe to execute. And there is a bytecode verifier in JVM, From the bytecode verifier it checks;

Whether this comes from a valid compiler.

Whether this has a correct format.

Whether this class file has a correct structure.

And if all of these are not satisfied it will throw a “Verifier Exception”. And if it successes it goes to the next which is Preparation.

2. Preparation: In this phase, if we use any instance level variable or a static variable in the class it will assign default values.

That means if it is int it will assign 0, if it is boolean it will assign false and if it is an object it will assign null.

3. Resolution: In this phase, it will replace the symbolic link with the direct link.

Example 1: We have “Employee”, “Student1”, “Animal” .. etc like objects. But machines do not understand those and, it will consider all those objects as Business objects or Domain-specific objects. As JVM doesn't understand who this Employee, Student, etc, Then this JVM replaces those symbolic links with the direct link before reaching the machine level.

Example 2: Employee a = new Employee();

So here in the resolution part, it replaces this everywhere we use Employee with the specific memory location which was reserved by this newly created Employee object.

🔹 Initialization: In here it assigns real value. And if there is a static block, it will execute. And every class must be initialized before it is actively used.

Some points consider the active use of the class.

1️⃣ new keyword

2️⃣ invoke a static method

3️⃣ assign values to static field

4️⃣ if the class is an initial class(have a main method)

5️⃣ if using a reflection API to load the class(getInstance)

6️⃣ instance subclass

If the class is subjected to any of the above it should go through the initialization process if it is not, then it is not required to go through the initialization process.

4 Ways to initialize a class:

✸ Use new keyword

✸ Use clone() method(clone()→ refers to creation of a exact copy of a class object)

✸ Use reflection API (get instance)

✸ Use Io.objectinputstream class

When initializing a child class, the parent class gets initialized! WHY❓❓❓❓

When compiling the JVM creates init() method to each of the constructors of our class.

So “init()” has — > code to invoke the default constructor’s init method and has codes to initialize instance variables and finally it has bytecode for the particular implementation.

And if there was no this() in the Constructor, at that time“init()” has → code to invoke the parent class-default constructor’s init method and has codes to initialize instance variables, and finally, it has bytecode for the particular implementation.

So there can have 3 kinds of codes in the init method, they are

Execution Engine

Execution of the Bytecode happens here.

Photo illustration: Components of Execution Engine

🔹 Interpreter- Read and interprets (converts) the bytecode to the machine code. And executes one by one sequentially.

🔹 JIT Compiler- This was introduced to overcome the disadvantage of the Interpreter, which is the“slow execution”. And JIT upgrades the performance of the java program by compiling the bytecode into native machine code at runtime.

🔹 Garbage collector- It automatically determines the memory areas that are not in use by the java program and it recycles those memory areas for other uses.

Data Types in JVM

There are 2 data types in JVM as Primitive Types and Reference Types.

Primitive Types

It holds the values of data. There are 3 types as Numeric, Boolean, and Return Address.

🔹 Numeric: Again divided into 2 as Floating and Integral Types.

Floating is divided into Float and Double.

Integral is divided into 5 as Byte, Short, Int, Long, and Char.

🔹 Boolean: This represents by int or byte. Boolean False is represented as Zero and True is represent as Non-Zero. If it is an Operatable Boolean it is represented by int value. If it is an array of Boolean, then it is represented as an array of Byte.

🔹 Return Address: This is not accessible to developers. And this is a specific type to implement final.

Reference Types

It holds references to objects. There are 3 types as Class type reference, Object type reference, and Array type reference.

🔹 Class type reference: Reference to an instance of the class.

🔹 Object type reference: Reference to class implementation.

🔹 Array type reference: Reference to Array.

Photo illustration: JVM data types

Word Size

Word size is just a base/unit. Use to indicate how much memory the processor can be addressed. (At least there should be 32-bit sized). The length of a word size is decided by a particular implementation.

2 Rules to decide the Word size

  1. Should be able to hold any Primitive data type.
  2. Two words should be able to carry long or double values.

--

--