JVM Every Programmer Should Know

JVM — Simply called Java Virtual Machines. When we think about virtual machines we have two types. System based & Application based virtual machines.

  1. System based Virtual Machine — This is a virtual machine that provides everything that needs to run an operating system. Means it provides memory, storage, OS, CPU Cores etc. So this is a real machine which works virtually. This allows to create independent multiple environments for multiple users. eg: hypervision — one host support multiple guests by sharing resources, memory, processors. Xem- allows multiple operating systems on a same computer and share hard ware components concurrently
  2. Application based Virtual machines — This is application which runs on an operating system and it provides platform to run other programs. That is how java achieves code once run anywhere. So Java is platform independent but JRE is platform dependent, Which is the implementation of JVM. eg: JVM — Platform for java, PVM — Platform for Dynamic Language, CLR — Help dotnet to create programs.

So from Application based virtual machine what it simply do is it convert file from one language to another language which some one able to understand. This does not involve any hardware components. And we can say this as a program, Complete process, So we can known as process based virtual machine.

JVM

On the movement you you start java program it crates JVM instance. On the movement program exists JVM dies. Actually JVM dies when there is no any non-demon threads or application suicides(System.exit()).

Javac hello.java — this is used to compile the code it is done by java compiler. Java hello — This is used to run, when we press enter it tells OS to give a JVM instance.

JVM Processes

JVM the main processers are Loading, Store and Executing

  1. Loading — The main responsibility is to load java class to memory area. In this process it reads the full class name, read variable information, read immediate parent information, check whether this is a class, interface or enum. And this loads to memory.
  2. Linking — In linking stage there are three main parts verification, preparation, resolution.
  3. Initialization(Last part of Loading) — In this step it rehanged all the default values to initial values.

2.1- Verification — In this step it verify that the source code is safe to execute. This is done when we try to load the class. Byte code verifier check the code and may assure this comes from valid compiler and check the structure, format and etc. If this phase fail it throw Java runtime exception verify exception.

2.2 Preparation — If the class has initial values or static variables and change the actual values to default values.

2.3 Resolution — In this step it maps the objects with the other classes where this is used. eg: In an student, enroll exam system, When we Student s = new Student(); this s object is use in exam class, result class also so s object maps with all the other classes wich use student object.

Method Area

In here it load all the class information. There is only one method are per VM. So basically it has one method are per jvm. But if we have 5 java programs running that means it has 5 jvms and it has one method are per each JVM.

Heap Area

In here it has all the objects and static variables. This is also like Method Area it also has one heap area per VM.

Stack

This loads method information. There is one stack per Thread. For a single application there is one or more threads. When application begins there is non-demon thread whose responsibility to execute main method.

Lets see how the stack is looks like in these steps ………………..

Step 1 — In the beginning lets assume stack has no any data and when it comes to step 1 it store x variable inside the stack.

Step 2 — Now stack has the variable of x and its default value. But in step 2 it removes the x variable from the stack because the method is over.

Step 3 — When coming to step 3 we don't have any values in the stack. So now on the step 3 it add the y variable into stack.

Step 4 — Now when coming to step 4 it has variable y and in step 4 the method is over so it removes the y variable.

So this is y we cant access one method from another method if we do not specifically give the access. So when coming to method B after method a stack don’t have any information of method A.

Native Method Area

So this is the place which keeps all the methods written in other languages. So when the method want to access native method it directly take from native method area. Fo example this methods can be c,c++ etc. Below is how it works in the process.

PC Register

PC Register is the one holds information about the next executions.

In the above diagram we can PC Register hold the information of thread 1 and thread 3 but not thread 2, because thread 2 holds information of native methods so pc register not hold information of native methods.

Execution

All these things do before active use case. What are the active use cases.

  1. when object is created using new keyword.
  2. Invoke of static methods
  3. Assigning values for static fields.
  4. invoke of the main class
  5. reflection api to create objects.
  6. when instantiating subclass(when create subclass object it auto initialize parent)

Conclusion

So these are the main steps how jvm load the class and store for the execution. So lets discuss further details on next blogs. Thanks for your time and hope you would gain some knowledge from this article. :)

Blogger, Software Engineer, Racer, Gamer, Traveller

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creator of the Week: Rapid app development for a school’s COVID screening

#MacPawInternship 2019

The Best Free Tools to Just Get it Done

SSRF (server-side request forgery)

Why We Backed GraphCMS

Using OCR technology to support the KYC process with Tesseract & Golang

Linux Essentials with Kali Linux | Day #1

Tips to Prepare your Documentation for Translation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chandima Jayamina

Chandima Jayamina

Blogger, Software Engineer, Racer, Gamer, Traveller

More from Medium

Why Java is still a great programming language

Duke Cheers

Multithreading in Java

What is this JVM ??