What is Java Virtual Machine?

Issath Sesni
5 min readMay 12, 2021

--

I hope newbies for the software engineering field have an idea about Java Virtual Machine (JVM). But most of them are failed to dive into JVM. Therefore, I am going to explain clear idea about JVM. First thing we want know what is virtual machine?

Virtual Machine (VM)

Virtual means that is not physically exist. Machine is something that makes our work easier. We can divide VM into two parts.

  1. System Virtual Machine :- One or some hardware creates multiple independent environments. Eg. Hypervisor, Xen
  2. Application Virtual Machine (Process VM) :- It creates a platform to run other programs. No hardware involves. It converts inputs into different outputs. Eg. JVM, CLR, PVM

So, if we want to work with JVM, we can’t install JVM, instead we want to install either JRE or JDK. If we a developer we want to install JDK , if you want to just run an application JRE is enough. JRE is platform dependent. When we run the java bytecode(.class file), JRE will deploy the code to create JVM instance. Every java program will create one JVM instance.

JVM will do 3 jobs to convert bytecode into machine code.

  1. Loading :- Class Loader
  2. Storing :- Memory area
  3. Executing :- Execution engine
JVM Internal Architecture

Now lets look into each part.

Class Loader

There are 3 steps to load the class.

  1. Loading
  2. Linking
  3. Initialization

Eg. Bootstrap class loader, Extension class loader, Application class loader.

Loading

When we load the class into JVM, it checks the following things :

  • Fully qualified class name
  • Instance variable information
  • Immediate parent information
  • Whether it is a class or interface or enum.

After this, it creates Class type object (type is Class) and stored in heap.

Linking

There are 3 steps under linking process.

  1. Verification :- Bytecode verifier will check whether the class is safe to execute. Therefore, it should pass the following conditions i.e correct structure, correct format, valid compiler. Otherwise it will throw runtime exception called verifier exception.
  2. Preparation :- Assign default values to static and instance variables. Eg. default value for integer is 0, Boolean is false.
  3. Resolution :- In java program we create objects with its real names. But machine will not understand this. So JVM will replace symbolic link with its direct link i.e replace the object with its memory address.
Resolution

Initialization

Assign real values(actual value) to static and instance variables. And static block is also executed. Initialization process should be executed before the following 6 active uses.

  1. Use of a new keyword
  2. Invoking static method
  3. Assigning value to static variable
  4. Invoking reflection API - getInstance()
  5. Invoking main method (Initial class)
  6. Instantiating sub class.

There are 4 ways to initialize a class.

  1. using new keyword
  2. clone()
  3. getInstance() - Reflection API
  4. IO.ObjectInputStream

Memory Area

There are 5 parts in JVM internal memory.

Memory area
  1. Method area :- Class information, type information, static and instance variable information will be stored in this area. JVM will create only one method area for a program execution.
  2. Heap area :- Object references will be stored in this area. All objects data including string and array are also stored.
  3. Stack :- Method information, local variable information will be stored. Stack frame will be created for each method. When a method is finished, that frame will pop out from stack. So the particular method’s local variables will no longer exist.
  4. PC register :- Hold next method execution information. i.e it holds order of execution. PC register is created per thread. When a method uses native method, that thread will have null value in pc register.

5. Native method area :- Native method information will be stored. When java does not provide functionality that you want to use, there you have to use native method, that is implemented in other programming languages. (C/C++).

Execution Engine

This is the final job of JVM. It has 3 components.

  1. Interpreter :- It reads the bytecode and interprets(convert) into machine code(native code) line by line and executes in a sequential manner. If one method is used multiple times, it interprets that method multiple times, this reduces the system performance and makes slow execution. To overcome this problem JIT compiler is used parallelly with interpreter.
  2. JIT compiler (Just in Time compiler) :- It improves the performance of java program by compiling bytecode into machine code at runtime. It directly calls the compiled code in which places the repeated method is used, this reduces the amount of time needed for compilation. When JVM first starts up, thousands of methods are invoked, thus significantly affects start up time, even if the end result is very good performance optimization.
  3. Garbage collector :- It is a daemon thread which runs in background, manages the memory automatically. It frees up the heap memory by destroying unreachable methods. i.e removing unreferenced objects, self referenced objects.

We have Java Native Interface(JNI) to provide native libraries to the execution engine. It connects execution engine with native libraries which are written in other programming languages(C/C++) while executing.

JVM is specification and JRE is implementation. And JVM physically does not exist. That’s all about JVM.

Comment out If I missed any points and do let me know your suggestions in the comment section below.

--

--