Monday, December 15, 2014

JVM Architecture

JVM Architecture (Java Virtual Machine):

JVM is a platform-independent execution environment that converts Java byte code into machine language and executes it. Most of the programming languages source code is directly converts machine code that is designed to run on a specific microprocessor architecture or operating system, such as Windows or UNIX.


Various part of JVM architecture:
  1. Class loader sub-system
  2. Various memory areas
  3. Execution Engine

Class loader sub-system:

Class loader sub-system is the responsible to load the class file into memory Class loader sub-system is classified into three,

  1. Loading
  2. Linking
  3. Initialization
Loading:
There are three types of loaders are there
  1. Bootstrap class loader:
    Bootstrap class loader is the responsible to load the class from bootstrap class path (rt.jar) . rt.jar contains all code java API classes.
  2. Extension class loader:
    Extension class loader is the responsible to load the extension class, that is classes present inside jdk/jar/lib/ext folder
  3. Application class loader:
    Application class loader is the responsible to load the classes from application level class path
Bootstrap loaded is high priory loaded among all three loaders

Linking:
Liking process also classified into three,
  1. Verify:
  2. Verify is the process to verify the whether generated byte codes are proper or not, whether byte codes are generated by valid compiler or not. If verification is failed, verify error will be thrown.
  3. Prepare:
  4. In prepare process, memory will be allocated for static variables and assigned with default values.
  5. Resolve:
  6. In resolve process, all symbolic references are replaced with original memory area references
Initialization:
In initialization process, original values are assigned to static variables and static block will the executed

Various memory areas:

Memory areas are classified into five,
  1. Method area
  2. Heap memory
  3. Stack area
  4. PC Registers
  5. Native Method stacks
Method area:
In method area, class level data and static variable data will be present. Whenever variable is declared as static, then memory will be allocated in method area to store that static value.

Heap memory:
In heap memory, object and corresponding instance variables data will be present. Whenever creating an object, memory will be allocated in heap memory to store that object.

Stack area:
For every thread, there will a separate stack created, each entry in the stack is called stack frame Stack frame contains 3 parts,
  1. Local variable array:
  2. Local variable array will have the local variables and its values of a method
  3. Operand stack:
  4. Operand stack act as a workspace for an intermediate operations
  5. Frame data:
  6. All symbols which all are used in that method will be handled in Frame data
PC Registers:
For every thread, there will a separate PC Register created, PC Registers are the responsible to hold address of current executing instruction, once current executing instructions are completed, PC registers will updated automatically to next executing instruction

Native Method stacks:
For every thread, there will a separate Native method stack created Native Method stacks are the responsible to hold native method information

Execution Engine:

Execution Engine is the responsible to executes the program Execution Engine mainly classified as below,
  1. Interpreter
  2. JIT compiler
  3. Garbage collector
  4. Java Native Interface (JNI)
Interpreter:
interpreter is the responsible to read, interpret and execute the program line by line

JIT compiler:
JIT compiler is used to convert the byte code into machine code once and this machine code can be used further instead of interpreting every time So this can be used for most frequently using methods. There is one more component in JVM called Profiler. Profiler is the responsible to identify the most frequently using methods
JIT compiler again classified as below,
  1. Intermediate code generator:
  2. Intermediate code generator is the responsible to produce as intermediate code
  3. Code optimiser:
  4. Code optimiser is the responsible to optimise the code
  5. Target Code generator:
  6. Target Code generator is the responsible to generate machine code or Native code
Garbage Collector:
Garbage collector is the responsible to recognizing when allocated objects are no longer needed, de-allocating (freeing) the memory used by such objects, and making it available for subsequent allocations

Java Native Interface (JNI):
Java Native Interface is the responsible to provide the native method library information.

3 comments: