Wednesday 12 April 2023

JVM - Basics #01

JVM

Java Virtual Machine implements Write once Run Anywhere approach 

Supports many Scala, Kotlin in addition to Java

-> VM -> OS -> Application 

Virtual M/C : H/W-> Hypervisor-> -> VM -> OS -> App.. 

-> VM -> OS -> App..

Java - Compiler - SC (Java) - > Byte code (class) -> 

       

JVM

Class Loader : Prepares the java classes and loads them into main memory.

Run Time Mem/ Data Area: Holds the the rT vars and Data

Execution Engine : Executes the code


Class Loader : 

Loading :  Boot strap Loader, Extension Class Loader, Application Class Loader

        Bin representation :

         Boot Strap Loader . Root Class Loader .. 

Super class of Extension loads the std java packages like java.lang, java.net, java.util, java.io                 etc.

The above are available in rt.jar file

         Extension Class Loader : Sub class of Bootstrap and the super class of thge Application class                                                            Loader.

Loads the files present in $JAVA_HOME/jre/lib/ext directory

Application Class Loader : subclass extensions loader, Loads the files present on the class                                                                  path

By Default, the class path is set to the current directory of th application.

Linking :  

Verification : Verify, Prepare and Resolve : check the structureal correctness of the .class file                                        by checking it against a set of constraint rules . Check for Verify                                                             Exception..

Preperation  : JVM allocates memory for stat fields of a class or interface. Initializes them                                          with default values.

Resolution   : Symbolic reference are substituted with direct references present in RT constant                                        pool.

         Initialization : 

Clinit .

                        Assigning values, constructor call, stat block exec, 
final stage of class loading. Also check for concurrency issues.
Handle thread safety issues for secured process.


Run Time Data Area:


Method Area : 

Class level data as RT Constant Pool, field and code for methods. Memory is insufficent for the startup, JVM throws outofMemoryError

Memory for class instances  and arrays is allocated.

Heap Area   :

All the objects and their corresponding instance varaiables and arrays are stored here. Created on startup. Only one heap Area/JVM

Stack Area  :

Local varsiables and temp results are stored here.
For every method call,  one entry is made into stack frame:
 Further divided in to local vars, operand stack, frame data    

PC Register : Program Counter Register. JVM supports multiple threads. Each thread has its own                                 PCR, to hold the address of currently executing JVM Instruction.

      Once executed, PCR is updated

Native Method Stack : JVM supports native methods. ( written in c/C++)

 

Execution Engine :

Byte code is loaded then interpreter 

       Reads BC line by line.
       JIT compiler : When EE finds the repeated code, it uses JIT-C, tot compile the en tire BC and                                        change it to native code.
JITC Consists of 3 components : Intermediate code Generator, Code Optimizer, Target Code                                                                      Generator.

Garbage Collector:

GC collects and removes un referenced objects.
Involves 2 phases : Mark and Sweep. Done automaticall by GC.
3 types of JC in KJVM, Serial, Parellel(through put)  and G1 (for large heap sizes > 4 GB)

  Java Native Interface 

Supports execution of native code via JNI (c, C++ or ASM files).

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...