Deep Java Virtual machine-class loading

Source: Internet
Author: User

  1. Life cycle of a class
    The life cycle of a class starts from being loaded into the memory of the virtual machine until it unloads out of memory, including: loading, validating, preparing, parsing, initializing, using, and unloading 7 stages. Where validation, preparation, parsing 3 parts collectively referred to as the connection. The order of occurrence is as follows:
    The 5 stages of loading, validating, preparing, initializing, and unloading begin in sequence, and in some cases the parsing phase can begin after the initialization phase, in order to support runtime bindings for the Java language (also known as dynamic binding or late binding).

  2. When is it initialized?
    When a class starts loading the first stage of the load, the Java Virtual Machine specification does not enforce constraints, but the virtual machine specification strictly stipulates that there are only 5 cases where the class must be "initialized" (and loaded, validated, and ready to begin) immediately.
    1) When encountering new, getstatic, Putstatic, or invokestatic 4 bytecode directives, if the class is not initialized, the initialization needs to be triggered first. The most common scenario for generating these 4 instructions is when you create an instance object with the New keyword, When reading or setting a static field of a class (except for static fields where the final decoration has placed the result in a constant pool at compile time), and when a static method of a class is called.

    2) When a reflection call is made to a class using the Java.lang.reflect package method
    3) When you first start a class, if you find that its parent has not yet been initialized, you need to trigger the initialization of its parent class first.
    4) When the virtual machine starts, the user needs to specify a main class to execute (the class that contains the main () method), and the virtual opportunity initializes the main class first.
    5) When using JDK1.7 's dynamic language support, if a java.lang.invoke.MethodHandle instance finally resolves the result ref_getstatic,ref_putstatic,ref_ Invokestaticde the method handle, and the class for the method handle has not been initialized, it is necessary to trigger its initialization first.
    The behavior of the above 5 scenarios is called an active reference to a class. In addition, all methods of referencing a class do not trigger initialization, called a passive reference.

    The loading process of the interface is slightly different, when a class is initialized, the parent class is required to be initialized, but when an interface is initialized, it does not require all of its parent interface to be initialized, only when the parent interface is actually used.

    Examples of passive references are as follows:
  3. The loading process of the class
    1) Load
    During the load phase, the virtual machine needs to complete the following 3 things:










Deep Java Virtual machine-class loading

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.