Java Virtual Machine ( JVM ) – How does JVM work?

Java Virtual Machine JVM is an abstract machine which provides the environment for java bytecode where it can be executed.
JVM hides the hardware and operating systems. It provides a virtual machine in which Java bytecode generated from Java source code by java compilers is executed. Its implementation is known as JRE.

Java Virtual Machine (JVM) – How JVM works?

 

When a code is executed using command prompt or command terminal, instance of JVM is created.
The responsibilities of Class Loader sub-system are:

  • Loading
  • Linking
  • Initialization

In loading, Class Loader loads .class files and generate binary. The generated bytecode is stored in Class Area. JVM stores name of class and name of its parent class, whether it is interface, enum or a class, variables, modifiers and methods in Class Area. After loading the class, JVM creates object to represent the file in heap memory. An object is of type Class that is predefined in java.lang package. The object of a class can be used to access class level information by a programmer.

In linking, an interface or a class is combined in run-time state of JVM so it can be executed. Linking performs verification, preparation and resolution. Resolution is optional. During verification, .class file is verified whether it has a valid bytecode and has correct format. In case of failure of verification, run-time exception java.lang.VerifyError is thrown. After successful verification, preparation takes place in which memory is allocated to the variables in class and initialization of the memory to default values. Resolution process involves determining concrete values from symbolic references.

In initialization, method is executed for initialization of a class or an interface. All the static variables are initialized with their defined values. This process is executed from parent class to child class and top to bottom in a class.

A stack has blocks called activation record. Each activation record stores a method call. The variables of this method are stored in corresponding activation frame. For each thread, JVM creates a stack and when the thread execution is terminated, stack will be destroyed.

Each thread has a PC register which stores the address of currently executing instruction of a thread. To store native method information, for every thread, a Native Method Stack is created. Execution engine uses the bytecode line by line and executes instructions of a class. Native method interface provides an interface that allows to call native libraries of C/C++ by JVM which may be hard hardware specific. Java native libraries is a collection of native libraries that are required by the execution engine.

 

jvm java virtual machine
Internal Architecture of JVM

 

Leave a Reply

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