How Jvm Works

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,992评论 0 10
  • 小狗又犯错误了,瞧,它又乱撒尿了。妈妈生气地望着小狗,眼睛瞪得圆溜溜的,仿佛有一条闪电直劈出去,想要劈穿小...
    小小张_83690阅读 174评论 0 1
  • 一个人的的行为习惯受什么影响,我觉得是环境。 每个人都想做出改变,我也是如此。3个月前,我的状态很遭,经常熬夜,饮...
    夜梦南柯阅读 372评论 0 1
  • 今天和老郑学习的是同理心销售。说到这个同理心,就是要站在对方的角度和对方一个高度来思考问题。销售的案例越是高大上,...
    婷婷你若盛开蝴蝶自来阅读 316评论 0 0
  • 需求:点击一个按钮,modal一个控制器出来,该控制器不完全覆盖控制器。展开方式:按钮处从上往下展开,要求不能使得...
    alige阅读 723评论 0 2

友情链接更多精彩内容