Java Virtual Machine(JVM)

What is Java Virtual Machine(JVM)?

JVM(Java Virtual Machine) provides the runtime environment for a java application. JVM executes the bytecode(.class files) generated by java compiler.

Life Cycle of a Java Virtual Machine(JVM) instance

A JVM instance is created when the java application starts i.e., when we execute java command a new JVM instance is created. The JVM instance dies when the application ends i.e., when the execution of main method is completed.

Each java application runs inside one JVM instance.So for running 3 java applications JVM creates 3 different instances.

Java Virtual Machine(JVM) Architecture

Java Virtual Machine(JVM)

Class Loader Subsystem

It loads the .class files(byte code) into the JVM.The byte code is verified by the Byte Code Verifier before loading into JVM.Please visit Class Loaders for more details.

Runtime Data Areas

JVM needs memory to store stuff like bytecodes,objects,method parameters,method return values,local variables and intermediate computation results.JVM organises the memory it needs for running a program into different data areas.Let us look into these runtime data areas.

Method Area

The class bytecodes are loaded and stored into this area.It basically stores class structure like field,method data,code for methods and constructors.This area stores the static variables declared in the classes.It also stores runtime constant pool and interned strings.

Note – Java 7 onwards, string constant pool(interned strings) are stored in specialised area inside heap.

Runtime constant pool per class

Byte codes in Java require data, often this data is too large to store directly in the byte codes, instead it is stored in the constant pool and the byte code contains a reference to the constant pool.

Several types of data is stored in the constant pool including

  • numeric literals
  • string literals
  • class references
  • field references
  • method references

For more details please visit Method Area(Non-Heap Area)

Heap Area

It stores all the objects and arrays that are created by the java program.Every JVM instance has only one method area and one heap area.So,the method area and heap area are shared by all the threads running inside the JVM instance.

The heap size may be configured with the following VM options:

  • -Xmx<size> – to set the maximum Java heap size
  • -Xms<size> – to set the initial Java heap size

Please visit Heap for more details.

Java Stack Area

Each thread has its own java stack .A java stack contains frames.Each frame has the information/data of one method invocation.Whenever a thread invokes a method, a frame is created and the frame is pushed onto that thread’s stack.The stack frame is removed from the stack once the method execution is complete.

The frame also stores the local variables and the intermediate computation results and other data.

Program Counter(PC) Register

Each java thread has its own PC register. PC register has the address of the current instruction being executed by the thread.

Native Method Stacks

It stores all the native method codes which are needed by the java application.

Execution Engine

The Execution Engine is responsible for executing the program and it contains two parts.

Interpreter 

It reads, interprets and executes the bytecode instructions one by one. As it interprets and executes instructions one by one, it can quickly interpret one bytecode, but slowly executes the interpreted result.

JIT Compiler (just in time compiler)

The JIT compiler compensates for the slow execution of the interpreter. The execution engine runs the interpreter first, and at the appropriate time, the JIT compiler compiles the entire bytecode to native code. Now the execution engine no longer interprets the method, but directly executes the native code. Execution in native code is much faster than interpreting instructions one by one.

However, JIT compiler takes more time to compile the code than for the interpreter to interpret the code one by one. Therefore, if the method is to be executed just once, it is always better to interpret it instead of compiling. JVM internally checks how frequently the method is executed and uses JIT compiler to compile the method only when the frequency is higher than a certain level.

Screen Shot 2016-03-02 at 2.47.21 pm

Please visit JIT Compiler for more details.

Conclusion: Java Virtual Machine(JVM)

Java Virtual Machine (JVM) is the dynamic foundation for Java applications, executing bytecode and managing memory. Its architecture comprises components like the Class Loader for bytecode loading, Runtime Data Areas for memory allocation, and an Execution Engine with an Interpreter and JIT Compiler for execution optimization. Understanding the JVM’s intricacies empowers developers to optimize performance and troubleshoot applications effectively.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *