Internal Details of JVM

Internal Details of JVM

 

JVM (Java Virtual Machine):

JVM stands for Java Virtual Machine. The java virtual machine is considered as an abstract machine. The JVM is used to provide a run time environment in which we can run or execute the java byte code.

The Java Virtual machines (JVMs) are available for a number of hardware and software. All the three JVM, JDK and JRE are platform dependent because of the configuration of the operating systems are different in each. But Java as a programming language is independent of platform. The following are the tasks that are performed by the JVM (Java Virtual Machine):

  1. JVM is used to load the code.
  2. JVM is used to verify the code.
  3. JVM is used to execute the code.
  4. JVM is used to provide run time environment.

 

What is JVM?

The Java Virtual machine can be considered as a specification where its working can be specified. The implementation provider is independent to choose the algorithms. The implementation of the Java Virtual Machine was provided by Sun and other companies also.

The Java virtual machine is considered to be an implementation and the implementation of the JVM is known as JRE that is Java runtime environment.

The Java virtual machine is also the runtime instance that is when a java command is written on the command prompt for the purpose of running java class, an instance of the Java virtual machine is created.

 

What does JVM do?

The following are the tasks that are performed by the JVM:

  1. JVM is used to load the code.
  2. JVM is used to verify the code.
  3. JVM is used to execute the code.
  4. JVM is used to provide run time environment.

The following are the concepts for which the JVM provides definitions:

  1. JVM provides definition for the memory area.
  2. JVM provides definition for the format of the class file.
  3. JVM provides definition for the register set.
  4. JVM provides definition for the garbage collected heap.
  5. JVM provides definition for errors like fatal error.

 

Internal Architecture of JVM:

The internal architecture of the Java Virtual Machine contains a class loader, memory area and execution engine etc. Consider the following diagram which shows the internal architecture of the Java virtual machine:

internal-architecture-of-jvm

The above diagram shows the internal architecture of the Java virtual machine. Now consider the following to understand what is in the diagram:

 

Class Loader:

The class loader is used to load the class files of the Java program. The class loader is considered to be as the subsystem of the JVM.

 

Class (method) Area:

The class (method) area is used to store per class structure for example the code for functions and methods, and the data of the methods. It also includes the run time constant pool.

 

Heap:

Heap is considered as the area in which the objects are allocated in the run time.

 

Stack:

The Java stack is used to store frames. Java stack is used to hold the local variables and their results. The Java stack also takes part for calling the functions or methods and returns the values or variables. In java every thread has its own private JVM stack that was created at the same time when thread was created.

Whenever a method is invoked a new frame is created. The frames are destroyed when the invocation of the method is completed.

 

Program Counter register:

Program counter register is also known as PC register. The program counter register is used to contain the address of the Java virtual machine instruction that is executed currently.

 

Native Method Stack:

The native method stack has all the native methods that are used in the application.

 

Execution Engine:

The execution engine contains the following:

  1. Virtual processor.
  2. Interpreter that is used to read the java byte code stream and then execute the instruction.
  3. Just in Time compiler: The just in time compiler is used to improve the performance. The Just in time compiler compiles the byte codes that have similar functionality. The advantage of just in time compiler is that it reduces time for compilation process. The term compiler is used to translate the instruction set of Java Virtual Machine to the instruction set of the computer system.