Str1 = new String(” ABC “); The constant pool and method area are related to each other. The constant pool and method area are related to each other

What is a method area

Str1 = new String(” ABC “); The constant pool and method area are related to each other. The constant pool and method area are related to each other

The relation between method area, permanent generation and metacolor

HotSpot is used to explain the following

First, the method area is what the JVM specification calls it, and persistent generation and meta-space are the two concrete implementations HotSpot uses to implement the method area

JDK1.8 previously implemented the method area in the JVM specification using Perm permanence

JDK1.8 deprecates the permanent generation and changes it to a meta-space instead of deprecating the method area

The difference between the permanent generation and the meta space is that the meta space is not in the virtual machine memory, the local memory is used, the effort to merge HotSpot and JRockit VM is reduced, and the GC performance of this part of the space is not satisfactory

We have made clear the relation and difference between method area, permanent generation and metacolor. Now we will look at the relation and difference between three constant pools

Constant pool

String constant pool

Is a hash table that holds references to the resident string

String resident: The JVM, for performance purposes, places strings that can be determined at compile time in a block of memory in the String resident pool. String a = “ABC “; String b = new String(“def”); Both at compile time can identify the dominant strings “ABC” and “def”

When a string instance in the heap is referenced by the hash table, it is given the “resident string” status

The string constant pool is shared by all classes in the JVM

The string constant pool was moved from the permanent generation to the heap in JDK1.7

Class file constant pool

The class file contains the version, field, method, interface, and other description information of the class, as well as the constant pool, which is used to store all kinds of Literal and Symbolic References generated by the compiler. Notice that this is in the class file

Literals are what we call constant concepts, such as text strings, constant values declared final, and so on

A symbolic reference is a set of symbols that describe the referenced target. The symbol can be any literal, as long as it is used to unambiguously locate the target. It usually contains: the fully qualified name of the class, the name and descriptor of the field, and the name and descriptor of the method

So these are the things that represent the code content of this class

Run-time constant pool

JDK1.7 only moved the string constant pool to the heap. JDK1.8 deprecated the permanent generation and changed it to a meta-space, but the runtime constant pool was still moved out of JVM memory with the meta-space

When a class is loaded into memory, the JVM stores the contents of the class constant pool into the runtime constant pool, which also has one for each class and replaces symbolic references to the runtime constant pool with direct references during the parsing phase of the class load. This process involves looking up the string constant pool

The problem of creating a new String is only related to the heap and the String constant pool in the heap.

// There is an instance of "ABC" in the heap. Global StringTable holds a reference value of "ABC". Str2 = new String("def"); str2 = new String("def"); Str3 = "ABC "; str3 =" ABC "; String str4 = str2.intern(); // Call intern() to return a reference to "def" in StringTable. If not, add a reference to str2. Str5 = "def"; str5 = "def"; Str6 = new String("def"); str6 = new String("def"); System.out.println(str1 == str3); //true System.out.println(str2 == str4); //false System.out.println(str4 == str5); //true System.out.println(str5 == str2); //false System.out.println(str6 == str2); //false System.out.println(str6 == str2.intern()); //false System.out.println(str6.intern() == str2.intern()); //trueCopy the code