How Jvm Works

How Jvm Works
竹林蕊

2018.12.09 阅读 101 评论 0 喜欢 0

Class loader subsystem

It is mainly responsible for three activities

  • Loading
  • Linking
  • Initialization

loading

The Class loader reads the .class file ,generate the corresponding binary data and it in method area. For each .class file,Jvm stores following informations in method area.

  • Fully qualified name of the loaded class and its immediate parent class
  • Where .class file is related to Class or Interfacde or Enum
  • Modifier ,Variables and method information etc.

After loading class,Jvm creates an object of type Class to represent this file in the heap memory. Please note that this object if of type Class predefined in java.lang package .This Class object can be used by the programmer for getting class level information like name of class,parent name,methods and variables information etc.To get this object reference we can use getClass() method of the Object class

Linking

Performs verification,preparation,and (optionally) resolution

  • Verification:It ensures the correctness of .class file i.e. it check where this file is properly formatted and generated by valid compiler or not. If Verification fails.we get run-time exception java.lang.VerifyError.
  • Preparation:Jvm allocates memory for class variables and initializing the the memory of default values.
    -Resolution:It is the process of replacing symbolic references from the type with direct references.It is done by searching into method area to locate the referenced entity.

Initialization

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

In gneral,there are class loaders :

  • Bootstrap class loader:Every JVM implementation must have a bootstrap class loader, capable of loading trusted classes.It loads core java API classes present in JAVA_HOME/jre/lib directory.This path is popularly known as bootstrap path.It is implemented in native languages like C,C++.

  • Extension class loader: It is child of bootstrap class loader.It loads the classes present in the extensions directories JAVA_HOME/lib/jre/ext(Extension Path) or any other directory specified by the java.ext.dirs system property.It is implemented in java by the sun.misc.Launcher$ExtClassLoader class.

  • System/Application class loader:It is child of extension class loader .It is responsible to load classes from application class path.It internally uses Environment Variables which mapped to java.class.path. It is also implemented in Java by the sun.misc.Launcher$AppClassLoader class.

package com.lin.jvm;
// Java code to demonstrate Class Loader subsystem
public class Test {
   public static void main(String[] args) {
       // String class is loaded by bootstrap loader, and
       // bootstrap loader is not Java object, hence null 
       System.out.println(String.class.getClassLoader());

       // Test class is loaded by Application loader 
       System.out.println(Test.class.getClassLoader());
   }
}

Note:

JVM follow Delegation-Hierarchy principle to load classes.System class loader delegate loader request to extension class loader and extension class loader delegate request to boot-strap class loader.if class found in bootstrap path, class is loaded otherwise request again transfers to extension class loader and then system class loader. At last if system class loader fails to load class ,then we get run-time exception java.lang.ClassNotFoundException.

image.png

JVM memory

method area:

In method area, all class level information like class name, immediate parent class name, methods and variables information etc.are stored, including static variables.There is only one method area per JVM,and it is a shared resource.

Heap area:

Information of all objects is stored in heap area.There is also one Heap Area per JVM.It is also a shared resource.

Stack area:

For every thread,JVM create one run-time stack which is stored here.Every block of this stack is called activation record/stack frame which store methods calls.All local variables of that method are stored in their corresponding frame.After a thread terminate, it's run-time stack will be destroyed by JVM.It is not a shared resource.

PC Registers:

Stores address of current execution instruction of a thread.Obviously each thread has separate PC Registers.

Native method stacks:

For every thread, separate native stack is created.It stores native method information.

image

Execution Engine

Execution engine execute the .class (bytecode).It reads the byte-code line by line, use data and information present in various memory area and execute instructions.It can be classified in there parts:

  • Interpreter: It interprets the byte code line by line and then executes.The disvantage here is that when one method is called multiple times, every time interpretedtation is required.

  • Just-In-Time Compiler(JIT):It is used to increase efficiency of interpreter .It compiles the entire byte code and changes it to native code so whenever interpreter see related method calls,JIT provide direct native code for that part so re-interpretation is not required, thus efficiency is improved.

  • Garbage Collector:It destroy un-referenced objects.

Java Native Interface(JNI)

It is a interface which interacts with the Native method Libraries and provides the native libraries(C,C++) required for the execution.It enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.

Native Method Libraries

It is a collection of the Native Libraries(C,C++) which are required by the Execution Engine.

生活 转载请联系作者,并注明出处。

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

春光支付宝

支付宝

春光微信

微信


喜欢  |  0

0条评论