程序员

How Jvm Works

2018-12-08  本文已影响14人  丫头可乐

Class loader subsystem

It is mainly responsible for three activities

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.

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

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 :

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:

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.

上一篇 下一篇

猜你喜欢

热点阅读