Thursday, 5 November 2015

JVM Architecture

JVM -  Cornerstone of Java Platform. It helps achieve Java goals like
  • Platform independence
  • Security
  • Fast execution
It is called virtual as it is abstract computing machine.  It is like a real computing machine with instruction set that it executes. Java Byte Code is its instruction set.  Like real computing system, jvm manipulates memory at runtime.

Core Responsibilities of JVM 
  • Loading and interpreting bytecode
  • Security
  • Automatic memory management
Components
  • Specification 
    • Document that describes JVM features and how it should work. Anyone can use specification to implement their own jvm
    • Specifies BYTE code instruction set
  • JLS- Java Language Specification
  • Concrete Implementation of JVM
    • Oracle's Hotspot JVM
    • IBM's JVM
  • Runtime Instance
    • It is instance of concrete jvm implementation
    • eg: java Hello
      • an instance of JVM is created and loaded into memory
      • JVM inturn loads Hello program into memory and then executes it
      • Each java application runs inside a runtime instance of some concrete jvm
      • Runtime instance runs only one java application
  • Performance
    • Java bytecode is compact, compiled and optimized
    • JIT compilation
      • Identify frequently executed bytecode  ~ hotspots 
      • hot spots are executed by separate jvm component - jit compiler
        • JIT compiler converts hotspots into machine code
        • Machine code is then cached
        • It is also referred as dynamic compilation


JVM Basic Architecture



Step1: Class Loading








Linking:






Virtual Machine: It is a Software simulation of a machine(calculator) which can perform operations like Physical machine. It doesn't have physical existence

Types of Virtual Machines:
  • Hardware/ System based VM: Provides logical systems on the same computer with isolation of each other. eg: VMware, Cloud computing, Kernel based VM for linux
    • Advantage: Effective utilization of hardware resources.
  • Application based/Software based/Process based VM: Runtime engines to run a particular programming language applications. eg: JVM, CLR(Common Language Runtime for .net), pvm for perl,
JVM Basic Architecture
  • JVM is runtime engine to to run java based applications
  • It is an interpreter and is JRE. (JRE is part of JDK.)
  • It loads .class file and executes line by line. 
  • Jvm has 3 components - ClassLoader sub system, memory area, execution engine
    • ClassLoader Sub System is responsible for 3 activities
      • Loading 
        • Reads the class file and store corresponding binary data(byte code) to method area
        • It will store ClassName, ParentClassName, Modifiers, variables, methods, constructors and constants.
        • After loading, it creates a Class object to represent class level binary info on the heap memory.
      • Linking-
        • Verification- a) It(Byte code verifier) is process of ensuring that binary representation of a class is structurally correct.  b) whether .class file is generated by valid compiler and properly formatted or not
        • Preparation -Allocate memory for class level static variable and assign default values(in method area)
        • Resolution- It is the process of replacing all symbolic references used in class with original references from method area.
      • Initialization- Assign original values for class level static variables. Static blocks will be executed from top to bottom.

Class loader subsystem: Types of class loaders
  • BootStrap/Primordial class loader
    • This class loader is responsible for loading core java API classes i.e the classes present in rt.jar(jdk\jre\lib\rt.jar). This location is called bootstrap classpath i.e Bootstrap class loader is responsible to load classes from Bootstrap classpath. Bootstrap class loader is by default available in jvm. It is implemented in native languages like c & c++.
  • Extension class loader
    • It is child of bootstrap class loader. The class loader is responsible to load classes from Extension class path. (jdk\jre\lib\ext). This loader is implemented in java.
  • Application/System class loader
    • It is child of Extension class loader. It is responsible to load classes from application class path. It internally uses Environment variable class path. This loader is implemented in java.
How java class loader works
  • Class loader follows Delegation Hierarchy Principle.
  • When jvm come across a class, first it will check whether the corresponding class is already loaded or not.
  • If it is already loaded in method area, then jvm will use that loaded class.
  • It is it not already loaded then jvm requests class loader subsystem to load that particular class, then class loader sub system hand overs the request to Application Class Loader
  • Application Class Loader delegates the request to Extension class loader, and Extension class loader delegates the request to Bootstrap class loader.
  • Bootstrap class loader searches in bootstrap class path(jdk\jre\lib). If the specified class is available then it will be loaded. Otherwise bootstrap class loader delegates the request to Extension class loader.
  • Extension class loader searches in Extension class path(jdk\jre\lib\Ext). If the specified class is available then it will be loaded. Otherwise Extension class loader delegates the request to Application class loader.
  • Application class loader searches in Application class path. If the specified class is available then it will be loaded. Otherwise Runtime exception - Class Not Found Exception will be raised.
need of Customized class loader
 Memory areas of JVM
  • method area -- Class level data and static variables.
  • heap area -- Objects and instance variable, also arrays(array is an object in java)
  • stack area--For every thread, a runtime stack area will be created(for method calls). Each entry in stack is called stack frame. Stack frame consists of 3 parts
    • Local variable array
    • Operand stack -
    • frame data -
  • pc registers - one pc register for each thread. It is to hold address of current executing instruction.
  • native method stacks - Separate thread a separate runtime stack will be created to hold native method information.
Execution engine - It is central component of jvm like cpu
  •  Interpreter - responsible for read, interpret and execute java program line by line.
  • JIT compiler (to handle/avoid repeat method call interpretation).
    • Intermediate code generator - Produces intermediate code. 
    • Code optimizer
    • Target code generator - generate machine code
 Profiler: It identify hot spots(repeatedly required method).
JNI(Java Native interface) - provides java native method informatiod.

Customized Class Loader:(When default class loader doesn't meet our requirements.)
  • Default class loader loads the class only once. It doesn't load the class on fly. 
  • We may want the loader to load the latest version of .class file. In such cases, we may have to go for Customized class loader.
  • Custom class loader can be created by extending java.lang.ClassLoader class
eg:
  public class CustomClassLoader extends ClassLoader{
     public Class loadClass(String cname) throws ClassNotFoundException{
Check whether updated version available
    if updated version available, then load updated version and return corresponding Class Object
    Else return Class Object of already loaded .class
}
}

Note:
  Heap and Method area is shared memory. Hence, data saved in this is not thread safe. 

No comments:

Post a Comment