Friday, November 22, 2013

Java Virtual Machine - Loading, Linking and Initializing

Loading

  • Process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation.
  • (JVM Specification: ) Java Virtual Machine specification gives implementations flexibility in the timing of class and interface loading.
  • Internal data structures are populated including Method area.
  • An instance of class java.lang.Class for the class being loaded is created.
  • Information about a type that is stored in the internal data structures and accessed by invoking methods on the Class instance for that type.
  • If Class Loader encounters a problem, it is reported during linking phase (LinkageError) on first active use of the class.

Linking 

  • A class or interface is completely loaded before it is linked. 
  • Process of taking a class or interface and combining it into the run-time state of the Java Virtual Machine so that it can be executed.
  • Linking a class or interface involves verifying and preparing that class or interface, its direct superclass, its direct superinterfaces.
  • Dynamic linking involves locating classes, interfaces, fields, and methods referred to by symbolic references stored in the constant pool, and replacing the symbolic references with direct references.
  • Linking is divided into three sub-steps: verification, preparation, and resolution:
    • Verification ensures the type is properly formed and fit for use by the Java Virtual Machine.
      • Verification ensures that the binary representation of a class or interface is structurally correct.
      • Verification may cause additional classes and interfaces to be loaded but need not cause them to be verified or prepared.
      • Final classes/methods, abstract classes/methods validations are checked here.
    • Preparation involves allocating memory needed by the type, such as memory for any class variables.
      • Involves creating the static fields for a class or interface and initializing such fields to their default values (initializing to other values except default values is done in Initialization where explicit initializers are executed).
    • Resolution is the process of transforming symbolic references in the constant pool into direct references. 
      • It is an optional part of linking.
      • Implementations may delay the resolution step until each symbolic reference is actually used by the running program.
      • After verification, preparation, and (optionally) resolution are completed, the type is ready for initialization. Resolution, which may optionally take place after initialization.
  • (JVM Specification: ) Java Virtual Machine specification gives implementations flexibility in the timing of class and interface linking.

Initialization

  • Initialization of a class or interface consists of executing the class or interface initialization method <clinit>.
    • <clinit> is static and has no arguments and is for static initialization blocks.
    • If there is no initialization of class variables in the code, compiler will not generate <clinit> method
  • Class variables are initialized during Initialization phase.
  • Initialization of a class or interface requires careful synchronization, since some other thread may be trying to initialize the same class or interface at the same time.
  • (JVM Specification: ) All implementations must initialize each class and interface on its first active use.
  • Involves creating the static fields for a class or interface and initializing such fields to their default values (initializing to other values except default values is done in Initialization where explicit initializers are executed).

Class-In-Use (Working with Objects)

  • For each constructor in the source code of a class, the Java compiler generates one <init() method.
  • If the class declares no constructors explicitly, the compiler generates a default no-arg constructor that just invokes the superclassís no-arg constructor.
  • For every class except Object, an <init() method must begin with an invocation of another <init() method belonging either to the same class or to the direct superclass.
  • Instance initialization methods may be invoked only by the invokespecial instruction (§invokespecial), and only on uninitialized class instances.

Unloading Class

  • Java Virtual Machine will run a class's classFinalize() method (if the class declares one) before it unloads the class.
  • If the application has no references to the type, then the type can't affect the execution of program and it can be garbage collected.

Class Loader

  • At run time, a class or interface is determined by a its binary name and defining class loader both.
  • Class Loader deals with file system to load the files for the JVM, no other component in JVM needs to deal with file system.
  • All Java virtual machines include one class loader that is embedded in the virtual machine, called Primordial class loader.
  • Every Class object contains a reference to the ClassLoader that defined it.
    • Class.getClassLoader()
  • Class LoaderTestClass loaded by class loader A, is not the same class as the class LoaderTestClass loaded with class loader B.
  • There are following three types of class loaders:
    • Bootstrap Class Loader
    • System Class Loader
    • Extension Class Loader

Premordial (Bootstrap) Class Loader

  • It implements the default implementation of loadClass().
  • Written in native language.
  • JVM assumes that it has access to a repository of trusted classes which can be run by the VM without verification.
  • Java core API classes are loaded by the bootstrap (or primordial) Class Loader.
  • Bootstrap ClassLoader searches in the locations (directories and zip/jar files) specified in the sun.boot.class.path system property.
    • String bootClassPath = System.getProperty("sun.boot.class.path");
  • Types loaded through the primordial class loader will always be reachable and never be unloaded.

Application (System) Class Loader

  • Application-specific classes are loaded by the system (or application) ClassLoaders.
  • Our application's code is loaded using this.
  • The system ClassLoader searches for classes in the locations specified by the classpath (set as the java.class.path system property) command-line variable passed in when a JVM starts executing. 
    • String appClassPath = System.getProperty("java.class.path");
    • sun.misc.Launcher$AppClassLoader

Extensions Class Loader

  •  Loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property).
    • String extClassPath = System.getProperty("java.ext.dirs");
    • sun.misc.Launcher$ExtClassLoader

Custom (User-Defined) Class Loader

  • Subclass of the abstract class Java.lang.ClassLoader
  • Method loadClass is to be implemented.
    • loadClass should be synchronized.
    • Call findLoadedClass to check class is already loaded. 
    • If we have the raw bytes, call defineClass to turn them into a Class object.
    • If the resolve parameter is true, call resolveClass to resolve the Class object.
    • If class not found, throw a ClassNotFoundException.
  • Every loaded class needs to be linked. This is done using the ClassLoader.resolve() method. This method is final.
  • Following are the steps a class loader performs:
    • Check if the class was already loaded.
    • If not loaded, ask parent class loader to load the class. 
    • If parent class loader cannot load class, attempt to load it in this class loader.

Examples of Class Loaders:

  • java.net.URLClassLoader, 
  • java.security.SecureClassLoader

Related Terms and Concepts:

JVM Startup

  • The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader.
  • The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). 
  • In an implementation of the Java Virtual Machine, the initial class could be provided as a command line argument. Alternatively, the implementation could provide an initial class that sets up a class loader which in turn loads an application. 

JVM Dynamic Linking

  • The process of JVM loading your program's classes and interfaces and hooking them together.
  • Java Virtual Machine builds an internal web of interconnected classes and interfaces.

Binding

  • Binding is the process by which a function written in a language other than the Java programming language and implementing a native method is integrated into the Java Virtual Machine so that it can be executed.
  • Although this process is traditionally referred to as linking, the term binding is used in the specification to avoid confusion with linking of classes or interfaces by the Java Virtual Machine. 
Previous: Java Virtual Machine - Garbage Collection

No comments:

Post a Comment