Method areas and constant pools

Source: Internet
Author: User

has been plagued with something in the method area recently.

1. How does the method area store class file information and class file constant pool?

2.class file constant pool and what is the relationship between running a regular pool.

The method area is stored with information about the class, constants, and static variables, that is, the data of the class being compiled. This statement is in fact no problem, just too general. A more detailed view is that the method area holds the class version, fields, methods, interfaces, and constant pools. Literal values and symbolic references are stored in a constant pool.

Symbolic references include: 1. The fully qualified name of the class, 2. Field names and properties, 3. Method names and properties.

The following is a picture of the method area, class file information, class file constant pool and running regular pool.

The following figure is used to indicate what the method area class file information includes:

You can see the class file information in the method area includes: Magic number, version number, constant pool, class, parent class and interface array, fields, methods and other information, in fact, the class contains fields and methods of information.

The following chart is the type of data stored in the class file

Type Name Number
U4 Magic 1
U2 Minor_version 1
U2 Major_version 1
U2 Constant_pool_count 1
Cp_info Constant_pool Constant_pool_count-1
U2 Access_flags 1
U2 This_class 1
U2 Super_class 1
U2 Interfaces_count 1
U2 Interfaces Interfaces_count
U2 Fields_count 1
Field_info Fields Fields_count
U2 Methods_count 1
Method_info Methods Methods_count
U2 Attribute_count 1
Attribute_info Attributes Attributes_count


Here is a graph to represent what is stored in a constant pool:


Actually reverse-compile with a class file

The following is the original Java code

public class Testint {  
    private String str = "Hello";  
    void Printint () {  
        System.out.println (65535);  
    }  
} 

After the reverse compilation to get the class file is the following


It can be seen that the contents of the class file being decompile and the above mentioned are able to correspond. This solves the relationship between the class file and the static Chang (class file constant pool)

Relationship between static constant pool and dynamic constant pool and the difference

The static constant pool stores some literal and symbolic references to the method area when the class file is loaded by the Java virtual machine, including a string, a constant of the base type, a symbolic reference that actually refers to the string in the constant pool, but the symbolic reference is not a direct storage string, Instead, it stores the index of the string in the constant pool.

The dynamic constant pool is when the class file is loaded and completed, Java Virtual Opportunities transfer content from a static constant pool to a dynamic constant pool, and part of a symbolic reference to a static constant pool is converted to a direct reference, such as a static or private method of a class, an instance construct method, a parent class method, This is because these methods cannot be overridden by other versions, so the symbolic reference can be converted to a direct reference when it is loaded, and some other method converts the symbolic reference to a direct reference when the method is called for the first time.

Summarize:

The method area stores the information of the class file and the dynamic constant pool, and the information of the class file includes the class information and the static constant pool. The information of the class can be a framework for the content of the classes file, where the specific content is stored through a constant pool.

In addition to the contents of the static constant pool, the contents of the dynamic constant pool convert the symbolic reference in the static constant pool to a direct reference, and the contents of the dynamic constant pool are dynamically added. For example, the Intern method that calls string can add a string value to a string constant pool, where the string constant pool is contained in a dynamic constant pool, but after jdk1.8, the string constant pool is placed in the heap.

Here's an article that says it's better

http://blog.csdn.net/vegetable_bird_001/article/details/51278339

Https://www.cnblogs.com/holos/p/6603379.html





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.