JVM runtime and class loading process

Source: Internet
Author: User

Java's JVM's memory can be divided into 3 extents: heap (heap), stack (stack), and method Area (methods) (The Knowledge Point reference http://www.cnblogs.com/dingyingsi/p/3760730.html)

Heap Area:
1. All objects are stored, each containing a class-corresponding information. (The purpose of class is to get operation instructions)
2.JVM only one heap area (heap) is shared by all threads, and the heap does not hold basic types and object references, only the object itself
Stack area:
1. Each thread contains a stack where only objects of the underlying data type are saved and references to custom objects (not objects), objects are stored in the heap area
2. The data in each stack (original type and object reference) is private and other stacks are inaccessible.
3. The stack is divided into 3 parts: the basic type variable area, the execution environment context, the operation instruction area (holds the operation instruction).
Method Area:
1. Also called the static zone, like the heap, is shared by all threads. The method area contains all class and static variables.
2. The method area contains elements that are always unique throughout the program, such as the class,static variable.

Class loading mechanism

  The JVM loads the class file into memory and verifies, parses, and initializes the data, eventually creating a Java type that the JVM can use directly.

Load:

load the class file bytecode content into memory and convert the static data into a run-time binary data structure in the method area, generating a Java.lang.class object representing the class in the heap , as the access entry for the method area class data. This process requires the participation of the ClassLoader.

Links: The process of merging the Java class's binary code into the running state of the JVM

Validation: Ensure that the loaded class information is compliant with JVM planning and there are no security issues.

Prepare: a phase that formally allocates memory for a class variable (static variable) and sets the initial value of a class variable that will be allocated in the method area.

Resolution: The symbolic reference of a virtual machine constant pool is replaced by a direct reference procedure.

Initialization

The initialization phase is the process of executing the class constructor <clinit> () method . The class constructor <clinit> () method is generated by the compiler's automatic collection of assignment actions for all class variables in the class and statements in static statement blocks (static blocks).

When initializing a class, it is necessary to initialize its parent class if it finds that its parent class has not yet been initialized.

Virtual opportunities Ensure that a <clinit> () method of a class is properly locked and synchronized in a multithreaded environment

When accessing a static domain of a Java class, only the classes that actually declare the domain are initialized.

For example:

  

Results:

   

Diagram Process:

    

Example code:

      

1  PackageCom.huang.classLoader;2  Public classDemo01 {3     Static {4SYSTEM.OUT.PRINTLN ("Static initialization of Demo01");5     }6      Public Static voidMain (string[] args) {7System.out.println ("Demo01 's Main Method!"));8A A =NewA ();9 System.out.println (a.width);TenA A2 =NewA (); One     } A } - classAextendsa_father{ -      Public Static intwidth=100; the     Static { -SYSTEM.OUT.PRINTLN ("Static initialization of Class A"); -width = 300; -     } +      PublicA () { -System.out.println ("Create class object of a"); +     } A } at classa_father{ -     Static { -SYSTEM.OUT.PRINTLN ("Static initialization of A_father"); -     } -}

Results:

static initialization Demo01//Load Demo01demo01 Main method first ! Then call the Main method statically initialize A_father//Initialize A's parent class statically initialize Class A//and then initialize a
Create a Class object to create a class object//Do not need to reload Class A

Class is initialized:

  The active reference to the class (the initialization of the class must occur)

New object of a class

Calling static members of a class (except for final constants) and static methods

Reflection calls to a class using the Java.lang.reflect package method

When the virtual machine starts, Java Hello is bound to initialize the Hello class, which is plainly the first class where the Main method is started

When a class is initialized, if its parent class is not initialized, his parent class is initialized first

Passive reference to class (no initialization of class will occur)

When accessing a static domain, only classes that actually declare the domain are initialized

Referencing a static variable of a parent class through a subclass does not cause the subclass to be initialized

Class references are defined by an array and do not trigger initialization of this class

Reference constants do not trigger initialization of this class (constants are stored in the solid pool of the calling class during the compilation phase)

For example:

 PackageCom.huang.classLoader; Public classDemo01 {Static{System.out.println ("Static initialization of Demo01"); }     Public Static voidMain (string[] args)throwsClassNotFoundException {//Active referencing//new A ();//System.out.println (a.width);//class.forname ("Com.huang.classloader.a"); //Passive References//System.out.println (A.max);//a[] as = new A[10];System.out.println (B.width);//B is not initialized    }}classBextendsa{Static{System.out.println ("Static initialization of B"); }}classAextendsa_father{ Public Static Final intMAX = 100;  Public Static intwidth=100; Static{System.out.println ("Static initialization of Class A"); Width= 300; }     PublicA () {System.out.println ("Create class object of a"); }}classa_father{Static{System.out.println ("Static initialization of A_father"); }}

  

  

JVM runtime and class loading process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.