Class file structure

ClassFile {
    u4             magic; // The Class file flag
    u2             minor_version;// Small version number of Class
    u2             major_version;// Large version number of Class
    u2             constant_pool_count;// Number of constant pools
    cp_info        constant_pool[constant_pool_count-1];/ / constant pool
    u2             access_flags;// Access tag for Class
    u2             this_class;/ / the current class
    u2             super_class;/ / parent class
    u2             interfaces_count;/ / interface
    u2             interfaces[interfaces_count];// A class can implement multiple interfaces
    u2             fields_count;// Field attributes of the Class file
    field_info     fields[fields_count];// A class can have a field
    u2             methods_count;// The number of methods in the Class file
    method_info    methods[methods_count];// A class can have multiple methods
    u2             attributes_count;// The number of attributes in the property table of this class
    attribute_info attributes[attributes_count];// Set of properties
}
Copy the code

Static constant pool

Static constant pools are the constant pools in *.class files.

  • literal
  • The name and descriptor of the fully qualified name field of the symbol reference class and interface and the name and descriptor of the method
  • Benefits: Constant pool is to avoid frequent creation and destruction of objects that affect system performance, which implements object sharing.

What are direct references, symbolic references and literals?

  • Literal:int i = 1 ; string s = "abc"1 and ABC are both literals.
  • Symbolic References: Symbolic References describe the referenced target as a set of symbols, which can be any literal, as long as they are used to unambiguously locate the target. Method names, class names, and field names are symbolic references
  • Direct References: A Direct reference can be a pointer directly to the target, a relative offset, or a handle that can be indirectly located to the target. If there is a direct reference, the target of the reference must already exist in memory.

Run-time constant pool

Static methods, instance constructors, parent methods, private methods, etc., are converted from symbolic references to direct references. Since these methods cannot be overridden as other methods, symbolic references can be converted to direct references at this point. Other method words are converted to direct references when they are first called.

String constant pool

The presence of a string constant pool allows the JVM to improve performance and reduce memory overhead.

Whenever we use a literal (String s= “1”;) When a string constant is created, the JVM first checks the string constant pool, and if the string already exists in the pool, assigns the address of the string object to the reference S (which is on the Java stack). If the string does not exist in the constant pool, the string is instantiated and placed in the constant pool, and the address of the string object is assigned to reference S (which is in the Java stack). Whenever we use the keyword new (String s=new String(” 1 “);) Create string constants, the JVM will first check the string constant pool, if the string in the constant pool already exists, then no longer create the string in the string constant pool object, which directly create a copy of the object in the heap, and then address assigned to the heap object reference s, if the string does not exist in the constant pool, The string is instantiated and placed in the constant pool, a copy of the object is created in the heap, and the address of the object in the heap is assigned to reference S.

Changes from version to version

Different versions of static constant pools are in Class files

1.6

  • The runtime constant pool is in the Perm Gen area (that is, the method area).
  • The string constant pool is in the runtime constant pool.

1.7

  • The runtime constant pool is still in the Perm Gen area (also known as the method area). In the JDK7 version, the migration of permanent generations has already begun, moving symbolic references, for example, to the native heap (which can be understood as a local memory similar to a meta-space). Literals are moved to the Java Heap; The static variables of the class are moved to the Java Heap. However, the runtime constant pool still exists, but much of the content has been moved, and only the references that have been moved remain.
  • The string constant pool is allocated to the main part of the Java heap. The string constant pool is separated from the runtime constant pool.

1.8

  • The JVM has moved the run-time constant pool out of the method area and created an area in the Java Heap to house the run-time constant pool. At the same time, the permanent generation is removed and replaced by the metacolith. The meta-space is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta-space is limited only by local memory. It is mainly used to store some metadata.
  • The metadata space of the method area has been split to store metadata information almost exclusively for classes and classloaders
  • The string constant pool exists in the Java heap.

Thread shared content visible -Thread sharing of JVM runtime data areas.