What is JVM?
What is a virtual machine?
Java Virtual Machine (JVM) is not physically existed. It can not be downloaded, installed or uninstalled. It is just a program that comes with Java Run Time Environment (JRE). There are two types of virtual machines: system based Virtual Machines (SVM) and Application based Virtual Machines (AVM). SVM provides multiple instances using hardware. The hypervisor is an example of SVM. AVM provides platforms for running the program. It does not involve with hardware. JVM is one of the Application based Virtual machines.
What is a java virtual machine?
JVM is completely specifications, and it comes with JRE. When JRE is installed, code for creating JVM will be deployed. JVM is platform-independent, but JRE platform dependent. JVM is created when executing the java program. When Java bytecode is executed using the “java filename.java” command, a JVM instance will be given by the operating system. Then it creates a non-daemon thread. Also, JVM will die when the java program exit. If there is no non-daemon thread or self-killing by executing System. exit() method JVM will die.
JVM is a part of JRE. JRE is part of the Java Development Kit (JDK).
JVM contains main three parts. They are,
- Class loader — Loading the class file into JVM
- Memory area — Storing loaded class file
- Execution engine — Execute the stored file
Class loader
· Bootstrap class loader
· Custom class loader
There are three responsibilities of the class loader
- Loading class into the memory area
2. Linking
3. Initialization the classes.
1. Loading
The following steps are done when loading the class.
- Getting fully qualified class name
- Reading instance variable information
- Reading about Immediate parent information
- It checks whether is it Class, Interface or Enum
The class loader takes a unique class. If there are two or more classes with the same name, then the class loader takes one class. Then class loader creates an object from the “class” type and assigns that class (Employee) into that class type object, then put it in a heap.
One object for one class will be created. Then it looks for the immediate parent. If there is no parent to refer object will be the class.
2. Linking
Linking is a combination of three steps.
- Verification
- Preparation
- Resolution
Verification
There is a byte code verification on the JVM. It is done by checking if this byte code comes from a valid compiler, has its correct structure, and has its correct format. If the verifier fails, it will throw a verifier exception. It checks that can JVM safely execute bytecode or not?
Preparation
If there are instance or static level variables, then JVM assigns them with default values, not initial values. It is done at the preparation level.
Resolution
It stores newly created values in specific memory locations.
3. Initialization
It will assign actual values with default values assigned by the Preparation part. Also, executing static blocks in the class is done in this phase.
Memory Area of JVM.
When we consider about JVM memory area, there are five parts of the memory area. They are the Method area, Heap area, Stack, PC Registers, and Native method area.
Class loader takes class information to the method area. After loading the class information, object data go to the heap area. The stack will keep method information and local variables. In every JVM, there is only one method area and heap area. Also, there is one stack, one PC register and one native method per thread. PC registers will keep information about subsequent executions. The native method area holds the information on native methods.
Data types of the memory area.
There are two kinds of data types in the memory area. They are primitive and reference types.
JVM represents Booleans, either int or bytes. If it is operatable Boolean, it represents an int value. If it is an array of Boolean, it represents an array of bytes. Long takes 64 bits, but others are the same. The return address type is another primitive data type. It is used to represent the final data type. It is specific to JVM, so developers can not access it.
There are several reference data types. Class reference to classes of java program. Interface reference to class Implementations. Word size is special keyword. The particular implementations define word size. Word size data type should be able to store any primitive type. If we create a java stack with word size data type, local variables are put into the stack, and then we need to hold primitive types by one-word size and long or double will use two words size.