What is JVM? (Java Virtual Machine)

Mahinsa Bhanuka
5 min readJul 17, 2022

--

Before getting to the JVM, Let’s discuss what is Virtual Machine?

Virtual Machine (VM)…

A virtual machine is there is a machine that does not physically exist. There are 2 types of Virtual Machines, Let’s discuss that,

01. SVM (System Based Virtual Machine)

  • Have one or more multiple hardware
  • Creates multiple environments to work
  • Example- Hypervisor, Xen

02. AVM (Application Based Virtual Machine)

  • Doesn’t have any hardware
  • It may have an application or software which helps to create a platform to run other programs.
  • Example- JVM, CLR, PVM

Java Virtual Machine (JVM)…

  • JVM is completely specification. It specifies how things should be done.
  • When you download Java Run-time Environment(JRE), JVM comes with the JRE.
  • When you install JRE, it will deploy all the code which can create a JVM.
  • You know Java Is platform-independent but JRE is tightly platform-dependent.
  • When you start the java program it will create a JVM instance in the computer.
  • JVM takes care of converting your byte code into machine code.

As an example, if you have a java source file called HelloWorld.java, and we can use the javac HelloWorld.java command to compile that, then it creates a file containing byte code with HelloWorld.class file. then we can run this using the java helloworld command, then the computer OS creates a new JVM instance. when that JVM creates a non-daemon thread inside that. the JVM looks for a public static void main method and starts the execution process. when that non-daemon thread dies, JVM also dies.

There are two ways for JVM get to destroy,

  • If non-daemon thread are exit JVM dies.
  • The application can suicide or call the system.exit method then the JVM instance will die and exit.

JVM is born when your application run and JVM dies when your application ends.

JVM Component…

  • Class Loader (Loading)
  • Memory Area (Storing)
  • Execution Engine (Executing)

ClassLoader…

Classloader's main responsibility is taking the class and loading it into the memory area, this is mainly divided into 3 categories,

  • Loading
  • Linking
  • Initializing

Loading

When the class file is loaded into JVM, it checks the following steps,

  • It reads the fully qualified class name
  • it reads variable information
  • It reads about immediate parent information
  • It reads whether this is a class, interface, or enum.

It will go and read that information and then loaded into the memory area. and also When it is loaded, JVM creates an object from the class type.

Verification

It has a bytecode verifier in JVM, which checks whether this class is safe to execute or not, it checks,

  • Whether this comes from a valid compiler
  • This has a correct structure
  • Code in a correct format

If all these are not satisfied, it will throw “Verifier Exception”. If it is a success then move on to the Preparation.

Preparation

Here, 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.

Resolution

We build objects with real names in the Java program. However, the machine will not understand this. As a result, JVM will replace the symbolic link with a direct link.

As an example, If we have “Vehicle”, “Employee”, “Animal” .. like objects. But machines can not understand those, It will consider all those objects as Business objects or Domain-specific objects.

Initializing

Here it assigns real values to static and instance variables. And if there is a static block, it will execute. And every class must be initialized before it is actively used. Here are some 6 active uses of the class,

  • New keyword
  • Invoke static method
  • Assign a value for the static field
  • If the class is an initial class (it has a main method)
  • If using the reflection API to create objects (getInstance)
  • Instantiating sub-class

There are 4 ways Java can Initialize its class,

01. Using a new keyword

02. Use a clone(); method.

03. Use a getInstance(); method (Reflection API).

04. Use IO.ObjectInputstream class

Memory Area…

The memory area in JVM is divided into 5 parts.

  • Method Area - A Java program’s class method area is the memory block that stores the class code, variable code (static variable, runtime constant), method code, and constructor. It stores every class’s class-level data, such as the runtime constant pool, field and method data, and method code.
  • Heap - The Heap area is the memory block where objects are created or objects are stored. Heap memory allocates memory for class interfaces and arrays (an array is an object). It is used to allocate memory to objects at run time
  • Stack - Method and local variable information will be saved. For each method, a stack frame will be built. When a method is completed, that frame will be removed from the stack. As a result, the local variables of the specific method will no longer exist.
  • Program Counter Register - A program counter register is associated with each JVM thread that performs the task of a certain method. The non-native method has a PC that stores the address of the available JVM instruction, whereas the value of the program counter in the native method is undefined. On some platforms, a PC register can store the return address or a native pointer.
  • Native Method Area - Also called C stacks, native method stacks are not written in Java language. This memory is allocated for each thread when it’s created And it can be of a fixed or dynamic nature.

Execution Engine…

Here is the final part of the JVM, this has 3 steps,

  • Interpreter - Read bytecode stream then execute the instructions.
  • JIT Compiler (Just In Time)- It is used to enhance performance. JIT compiles elements of the byte code that perform similar functions at the same time, reducing the amount of time required for compilation. The term “compiler” refers to a translator from a Java virtual machine’s (JVM) instruction set to the instruction set of a specific CPU.
  • Garbage Collector - This is a Java program that manages memory automatically. It is a daemon thread that operates in the background at all times. This essentially frees up heap memory by deleting unreachable methods.

Thank you…

References :

--

--