How JVM (Java Virtual Machine) Works

Nisal Jayathilaka
8 min readDec 27, 2021

What is JVM? JVM is converting bytecode into machine code. I know all of the developers reply to that question like this.

To answer that question you have to know what is virtual machine is? First, we have to know what is virtual: — Virtual is there is something that does not relay exits. Like VR box in VR box, we can get the experience that is not in the reality.

What is a machine: — system hardware or software that helps you to do your work easily. For example like a car, we can easily be done travel by using cars.

Before answering that what is JVM is we have to know in the computer system, we have two virtual machines.

1) SVM: — System based Virtual Machines

2) AVM: — Application base Virtual Machines

  • What is SVM: — SVM is one or some hardware that create an environment to work for users or instant. Example: — hypervisor, Xen hypervisor
  • What is AVM: — AVM is it doesn’t have any hardware involved but you may have software that helps you to create a platform to run other programs. Example: — JVM, CLR, PVM

Now let see what is JVM: — JVM is a complete specification it says how this should be done. Can we install JVM: — the answer is no. because it’s a virtual machine? It is inside the JRE (Java Runtime Environment)But we can install the JRE. If you have a doubt please search how to install JVM you can’t find it. Because it is inside the JRE. When you download and install the JRE it deployed a code to create a JVM for each environment. When you run your program, JVM takes care to read your class file and convert it to langue which your operating system understands.

We all know java is a program written once and run everywhere. Because JVM is platform-independent but JRE is platform dependent. Because we can’t install windows files into macOS or Linux that’s why.

JVM full path
Thread

Before going to the other notes let’s see what is the thread.

Thread nothing but it is a process inside the process. Example: — when you type in a Word document. It is typing and also checking the errors.

The process need address space but the thread does not need any address space.

When we go the process to process the time high and when we go the thread to thread it takes less time.

In the thread, we have two thread

1. Demon Thread

2. Non-Demon Thread

Daemon thread is a thread that provides the service to the non-demon thread.

Example: — Take school management system without management there is no student without the student no school.

How JVM is dead:- All the non-demon threads are dead automatically demon thread is also dead then the JVM is also dead. When your program exit your JVM also died.

JVM has 3 steps

  1. Method area: In the method area, all class level information like class name, immediate parent class name, methods and variables information etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource.
  2. Heap area: Information of all objects is stored in the heap area. There is also one Heap Area per JVM. It is also a shared resource.
  3. Stack area: For every thread, JVM creates one run-time stack which is stored here. Every block of this stack is called activation record/stack frame which stores methods call. All local variables of that method are stored in their corresponding frame. After a thread terminates, its run-time stack will be destroyed by JVM. It is not a shared resource.
  4. PC Registers: Store address of current execution instruction of a thread. Each thread has separate PC Registers.
  5. Native method stacks: For every thread, a separate native stack is created. It stores native method information.

Whenever we add the class to the .java file and compile it there is a .class file created according to the class. JVM takes the .class file

Memory in JVM

JVM has divided memory space between two parts one is Stack and another one is Heap space. Stack space is mainly used for storing the order of method execution and local variables.

Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks.

Stack

  • It’s a temporary memory allocation scheme where the data members are accessible only if the method ( ) that contained them is currently is running.
  • It allocates or de-allocates the memory automatically as soon as the corresponding method completes its execution.
  • We receive the corresponding error Java. lang. StackOverFlowError by JVM, if the stack memory is filled.
  • Stack memory allocation is considered safer as compared to heap memory allocation because the data stored can only be accessed by the owner thread.
  • Memory allocation and de-allocation are faster as compared to Heap-memory allocation.
  • Stack-memory has less storage space as compared to Heap-memory.

Heap

  • We receive the corresponding error message if Heap-space is full, java. lang.OutOfMemoryError by JVM.
  • This memory allocation scheme is different from the Stack-space allocation, here no automatic de-allocation feature is provided. We need to use a Garbage collector to remove the old unused objects to use the memory efficiently.
  • The processing time (Accessing time) of this memory is quite slow as compared to Stack-memory.
  • Heap-memory is also not threaded-safe as Stack-memory because data stored in Heap-memory are visible to all threads.
  • The size of Heap-memory is quite larger as compared to the Stack-memory.
  • Heap-memory is accessible or exists as long as the whole application (or java program) runs.

Garbage Collector

The garbage collector is reclaiming unused references. The garbage collector is a runtime function it is run always. It removes along with variables and functions.

ClassLoader

In the above we talk about JVM has three main parts first we talk about the Memory area in this section we talk about the ClassLoader.The class loader takes the class and is loaded to the memory. But it has more than that.

Classloader mainly has three categories

· Loading

· Linking

· Initialization

Classloader main responsibility is taking your class file and put into the memory area. Mainly we can find two types of class loaders in the memory area in (JVM)

  1. Bootstrap class loader
  2. The custom defined class loader
  • Loading: — the main responsibility is to take your class file ad put it into the memory. But when you going to do that you have done a different thing.

Imagine you have a java file you convert that to the class. In the class loaded mechanism what it does is take

· Fully qualified class name.

· Read about variable information

· Immediate parent information

· Read it as a class or interface or Enum.

It will go and read that information and then it loads into the memory area.

Every class loads into the JVM and it especially thing it creates an object from class type and put it into the heap.

  • Linking: — In linking, we have three steps.

· Verification

· Preparation

· Resolution

  1. Verification: — java has a bytecode verifier in JVM in here it will exactly. Check whether this class is safe to execute or not (that is why we tell java is safe to execute in any environment)

Byte code verifier check: -

· It comes from a valid compiler.

· Had correct structure

· Class file has the correct formatting

If any of them are not satisfied JVM gives runtime exception call verification exception. After verification, it goes to the perpetration.

2. Preparation: — If you use any instant or static variables in your program, preparation assigns the default value for that.

Example: — static int a =10;

In the preparation process, it will assign: — 0 value for that

3 . Resolution: — In java we can create a class like an Employee, Customer or Student when it is coming to the machine level there is nothing called employee and student. In there everything is business objects.

A business object is concreated object for your application. Before it reached the machine level JVM is replacing the symbolic link with a direct link.

Example: — Student student = new Student ();

student.prepration();

In the resolution part, JVM does is replaced everywhere which you used the student with the specific memory location which reserve by their newly created through the object.

Initialization:-In the initialization it will assign the real value and it will execute the block. There is a limitation JVM and enforce implementation JVM specifically force to the implementation. Initialization must do before each class active use.

What is the active use of class?

  • New keyword
  • Invoke static method (has static method)
  • Assign a value for static filed
  • Initialize class (class have a main method)
  • Refection framework to create an object.
  • The institution in a subclass: — mean call the constructor of a class that creates an instance or object of the type of the class. In other words, creating an object of the class is called instantiation.

Before these 6 create (Go with initialization phase) these 6 are active users and others are passive use they didn’t go with the initiation process.

Now you have an idea about how these JVM work and JVM is not about converting byte code into machine code.

--

--