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.
- 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
- 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.
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 the main processers are Loading, Store and Executing
- 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.
- Linking — In linking stage there are three main parts verification, preparation, resolution.
- 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.
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.
In here it has all the objects and static variables. This is also like Method Area it also has one heap area per VM.
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 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.
All these things do before active use case. What are the active use cases.
- when object is created using new keyword.
- Invoke of static methods
- Assigning values for static fields.
- invoke of the main class
- reflection api to create objects.
- when instantiating subclass(when create subclass object it auto initialize parent)
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. :)