JVM-classloader (for conversion)

Source: Internet
Author: User

The method area is used in the loading phase:

The method area is the runtime memory area that can be shared by each thread. Stores the structure information of each class, such as the runtime constant pool), field and method data, constructor and common method bytecode content, also includes some special methods used in class, instance, interface initialization.

If you think of the method code as its "static" part and the temporary data to be recorded for a method call as its "dynamic" part, the Code of each method is only one copy and stored in the JVM method area. Each time a method is called, A new stack frame is allocated to the java stack of the thread where the call is located to store temporary data. The stack frame is automatically revoked when the method returns.

 

In Java, the Class Loader loads a class into a Java Virtual Machine. Three steps are required: Loading, linking, and initialization, the links can be divided into verification, preparation, and resolution (optional ).
Load: Query and import binary data of classes or interfaces;
Link: perform the following verification, preparation, and resolution steps. The resolution steps are optional;
Verify: Check whether the binary data of the import class or interface is correct;
Preparation: allocate the required storage space to the class;
Resolution:Convert the symbol reference in the constant pool to a direct reference. Depending on actual needs, it can be postponed until after initialization.;
Initialization: Assign the correct initial values to the variables, such as activating static variables of the class, initializing Java code and static Java code blocks

2.1 Load

In the loading phase, the Java bytecode is read into the JVM memory in binary format, and then the binary data stream is parsed into the data structure of the runtime in the JVM according to the bytecode specification. Java only regulates bytecode and does not specify the internal runtime data structure. Different JVM implementations can adopt different data structures, these runtime data structures are stored in the JVM's method area (for details about the internal data structure definition of the hotspot JVM, refer to Saga's blog post using hotspot SA to get a glimpse of objects on permgen ). After the binary parsing of a class is complete, JVM will generate an Instance Object of the Java. Lang. class type on the stack. The content of this class in the method area can be accessed through this object.

The JVM specification does not specify how binary bytecode data is generated. In fact, to support the scalability of binary bytecode data sources, it provides a callback interface that allows you to obtain and describe the binary bytecode action of a class by using the full-qualified name of a class to the external implementation of JVM, this is the class loader we will talk about later. If necessary, we can customize some class loaders to achieve some special application scenarios. With JVM support, binary streams can be generated in the following ways:

(1) read from the local file system

(2) load from the Network (typical application: Java applet)

(3) load from jar, zip, war, and other compressed files

(4) dynamic compilation of Java source files (JSP dynamic compilation)

(5) generate directly through the program.

2.2 connection

The connection phase mainly involves verification after loading and preparation before initialization. It is divided into three phases.

2.2.1 Verification

Verification is the first step in the connection phase. It is mainly used to ensure that the loaded bytecode complies with the Java language specifications and will not bring harm to virtual machines. For example, verify whether the class complies with the bytecode format, whether the variables and methods are duplicated, whether the data type is valid, and whether the inheritance and implementation are compliant with the standards. The verification content can be subdivided into four phases: File Format verification (this step will cross the loading phase), metadata verification, bytecode verification, symbol reference verification (verification at this stage is often performed in conjunction with the parsing phase ).

2.2.2 preparation

The preparation phase mainly allocates memory for the static variables of the class and sets the default JVM initial values. For non-static variables, no memory is allocated to them.

The initial values of various types in JVM are as follows:

The default values of int, byte, Char, long, float, and double are 0.

Boolean is false (Int Is used inside JVM to represent Boolean, so the initial value is 0)

The reference type is null.

For the final static basic type or string type, use the constant value directly (this is actually handled in the compilation phase ).

2.2.3 Parsing

The parsing process is to find the class, field, method, and interface symbol references in the constant pool of the class, and replace them with the direct reference process.

A. the parsing process is mainly for the constant_class_info, constant_fieldref_info, constant_methodref_info, and constant_interfacemethodref_info constants in the constant pool.

B.The JVM specification does not specify the time when the parsing phase occurs.It only specifies the execution of anewarray, checkcast, getfield, getstatic, instanceof, invokeinterface, invokespecial, invokespecial, invokestatic, invokevirtual, multinewaary, new, putfield, putstatic: when these 13 commands are used in symbolic commands, parse them first to obtain their direct reference.

C. JVM creates a runtime constant pool internally for each loaded class (see the figure above). Prior to parsing, the symbolic reference is saved as a string in the runtime constant pool, runningWhen a symbolic reference is required, the parsing process is promoted., The parsing process is through the symbolReference to find the corresponding class object, and then replace the symbol reference with direct reference. Because the symbol reference has been replaced with a direct referenceWhen you access the service again, you do not need to parse the service again and directly return a reference.

2.3 Initialization

The initialization stage is to assign the correct initial values to the static variables of the Class Based on the initialization statement in the user program. The initialization execution logic will be reflected in the class constructor method <clinit>. This method is generated by the compiler in the compilation phase. It encapsulates two parts: the initialization Statement of static variables and the static statement block.

2.3.1 initialization execution time

The JVM specification clearly defines the initialization execution conditions. Initialization is performed only when one of the following four conditions is met.

(1) PassNew Keyword instantiation object,ReadOrSet static variables of the class,Call static methods of Classes(Corresponding to the four bytecode commands new, getstatic, putstatic, and invokespecial).

(2) When performing the above behavior through reflection.

(3) initialization of the parent class is triggered when the subclass is initialized.

(4) as the main class for direct running of the program entry.

2.3.2 initialization process

The initialization process includes two steps:

(1) If the class has a direct parent class and the parent class is not initialized, initialize the direct parent class.

(2) If the class currently has the <clinit> () method, execute the <clinit> () method.

It should be noted that the initialization of an interface does not require that its parent interface be initialized first. (The interface cannot have static blocks)

2.3.3 conditions for the <clinit> () method

Not every class has the <clinit> () method. The <clinit> () method is not available in the following cases:

A. The class does not have static variables or static statement blocks.

B. Although static variables are defined in the class, no explicit initialization statement is provided.

C. If the class contains only the initialization Statement of final static variables, and the initialization statement uses the compile time table, there will be no <clinit> () method.

2.3.4 concurrency

In the same classloader domain, each class is initialized only once,When multiple threads need to initialize the same class, only one thread is allowed to perform initialization, and other threads are waiting. After Initialization is completed, this thread will notify other waiting threads.

2.4 Distribution of classes and objects in the Method Area and stack during use
public class TestThread extends Thread implements Cloneable {    public static void main(String[] args) {        TestThread t = new TestThread();        t.start();    }}

In the above Code, the storage and reference of testthread and related classes running on JVM are shown in:

T in the local variable table of the main thread VM Stack

T, as a reference of the testthread object, is stored in the stack frame space of the online program. The class object instances corresponding to the thread object and type data are stored in the stack, and the type data is stored in the method area, as mentioned above, the symbolic references in testthread data will be replaced with direct references during parsing, therefore, testthread type data will be directly referenced to its parent class thread and Its implemented interface cloneable type data.

In the same class loader space, for classes with the same fully qualified names, only the unique instance and type data of the class will exist. In fact, the instance data of the class is referenced with its corresponding class object.

 

 

 

Http://my.oschina.net/tryUcatchUfinallyU/blog/144133

 

JVM-classloader (for conversion)

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.