top of page
Search

How JVM Works ?

  • Writer: Sudhasrinivas Pallam
    Sudhasrinivas Pallam
  • Aug 23, 2020
  • 2 min read

Updated: Jul 18, 2021


ree

JVM makes java code independent, JVM loads all the class files from the classpath. From the above image, we can see different components of JVM. Let try to see whats does they do




1. Class Loader Subsystem: Bootstrap class loader, extension class loader, System /Application class loader

  • Bootstrap Class loader: Loads all native classes or files rt.jar(runtime.jar)

  • Extension: loads Extension libs

  • Application: loads files from classpath


Classloader is primarily performed 3 activities

  • Loading: Classloader reads the .class file, JVM created an object of type class and save it in the heap memory, this object will be used to get class-level information such as class name, parent name, method, variables, etc.

  • Linking: performs verification, preparation, and option resolution.

Verification: Verifies the .class files, checks for initializations. If verification fails, we get a run time exception. The activity is done by the component ByteCode verifier. We can disable this check using java – noverify command Preparation: JVM allocates memory for class variables and initializing the memory to default values. Option resolution: It is the process of replacing symbolic references form the type with direct references. It is done by searching into the method area to allocate the referenced entity

  • Initialization: In this phase, all static class variables are assigned with their values defined in the code and static block if any. This is executed from top to bottom in a class and from parent to child in the class hierarchy


2. JVM Runtime Data Area:

  • Method Area: Stores all class level information like class name, parent name, methods and variables information, static variables, etc. There is only one method area per JVM and it is a shared resource

  • Heap Area: Information of all objects is created and stored in the heap area. It’s a shared memory

  • Stack Area: For every thread, JVM created one runtime stack which is stored here. Every block of this stack is called activation record/stack frame which stores method calls. All local variables of that method are stored in their corresponding frame. After a thread terminates its runtime stack will destroy by JVM. It is not a shared resource

  • PC Registers: Store address of current execution instruction of thread obviously, each thread has separate PC registry

  • Native method stack: For each thread, a separate stack is created. It stores native method information


3. Execution Engine: The execution engine executes bytecode (.class file). It reads byte code line by line uses data and information present in various memory areas and executes instructions. It can be classified into 4 parts

  • Interpreter: It interprets the byte code line by line and then executes it. This disadvantage here is that when one method so-called multiple times every time interpretation is required

  • Just In Time Compiler: It improves the performance of the interpreter. It compiles the entire bytecode and changes it to native code, whenever the interpreter sees repeated method calls JIT provides direct native code for that part so re-interpretation is not required

  • Garbage Collector: It clears up the memory by destroying the unreferenced objects

  • Java Native Interface: It is an interface that enables JVM to call Native libraries and is called by Native libraries which may be specific to the hardware


Thanks for reading!

 
 
 

Comments


©2022 by Sudhasrinivas Pallam

bottom of page