Finishing constant pool, a string constant pool

Since previous studies a bit JVM memory division, which method of constant pool area scanty, so you want to explore it

Look at this more clearly written 
https://blog.csdn.net/tophawk/article/details/78704074(jdk1.8 version) 
https://blog.csdn.net/youyou1543724847/article/details/52337257

Following a bit chaotic, yet tidy, with reference to the 
https://blog.csdn.net/zm13007310400/article/details/77534349 
https://www.cnblogs.com/holos/p/6603379.html 
HTTPS: / /blog.csdn.net/qq_31142587/article/details/78203382

main problem:

S1 = String "abc";
1
compile-time checking string constant pool has abc, direct return s1, abc references, if not abc, abc heap to create an object and a string constant increase in the pool of his references are in the process compile implement or below mentioned proven before generating an abc instance after preparation stage? ? It may be due to Jdk long version of the problem?

Methods and constant pool area
method area where we had stored version of the class, fields, methods, interfaces, and constant pool.

 
FIG below a class file for the method area showing what information comprises: 


class constant pool
we write each after being compiled Java class, will form a class file; in addition to the class file containing the class versions, fields, methods, interfaces, and other descriptive information, there is a pool of information is constant (constant pool table), various literal (the literal) for storing and generating a compile symbolic reference (symbolic references);
literal comprising: a text string 2. the value of the eight basic types of 3. declared final. constants, etc.;
symbolic references include: class 1 fully qualified name, field names and attributes 2, 3 and attribute method....
Each class has a class file constant pool.
class constant pool is at compile time of each class has, at compile time, is stored in a constant symbolic reference, that instance they exist not object.
Method of operation zone time constant pool
after the java files are compiled into class files, that will generate I said above class constant pool. Then the runtime constant pool is when to produce it? 
jvm in the implementation of a class, must be loaded, connect, initialize, and the connection also includes validation, preparation, analytical three stages. And after class when loaded into memory, jvm brings its class constant pool is stored in the runtime constant pool, runtime constant pool in memory, which is the class constant pool is loaded into memory later versions, different place: it's literal can dynamically add (String # intern ()), symbolic references can be resolved directly referenced. 
Runtime constant pool after the class is loaded, the symbols of each class constant pool reference value dump runtime constant pool, that is, each class has a runtime constant pool, after parsing class , replaced symbolic references to direct reference, the reference value of the global constant pool consistent. Query parsing process will go global string pool, which is what we call the following StringTable, in order to ensure that the runtime constant pool references cited a string of global string pool is the same.

Parsing process will go to the query string constant pool, which is mentioned above us StringTable, to ensure that the runtime constant pool quoted strings and string constant pool is the same.

Another important feature of the runtime constant pool file relative to the CLass with constant pool is dynamic, Java language does not require constant necessarily only compile to produce, that is not preset the content CLass file constant pool to enter the zone method runtime constant pool, during operation may be new constant into the pool, take advantage of this feature is the developer is more intern () method of the String class. (Research Intern ())

Constant pool of benefits
constant pool in order to avoid the frequent create and destroy objects affect the performance of the system, its implementation of the shared object. 
For example, a string constant pool, at compile time to put all the string literals into a constant pool.

Save memory space: all the same string constants constant pool are merged, occupies only one space.
Save operating time: When comparing strings, == ratio equals () quickly. For the two reference variables, only a == determines whether reference equality, it can be determined whether the actual values are equal.
String constant pool
string constant pool memory region in which the position of Java 
in JDK6.0 and earlier, the string constant pool is on Perm Gen zone (i.e. zone method); and
in JDK7.0 version string constants pool was moved to the heap. As to why the move within the heap, probably due to memory space method area is too small.
String constant pool of what is put 
in the JDK6.0 and earlier versions, String Pool are put string constants
in JDK7.0, due String # intern () has changed, and therefore can be stored in the String Pool put in the pile string object references 
should be noted that: the string constant pool there is only one!
example

S1 = String "hello, world!";
String s2 = "hello, world!";
. 1
2
After performing the first line of code that is completed, the existing reference constant pool "hello, world!", Then s2 does not constant pool apply for a new space, but directly to an existing string memory address returned to s2.

Global string constant pool (that is, a string constant pool, I think it is a global emphasize only one, shared by all classes) the contents of the class is loaded in, proven, after the preparatory stage in the heap generate a string object instance, and then the string object instance reference values stored in the string pool (remember: string pool is stored in a reference value instead of a specific instance of an object, the object is a specific example of an open heap of storage space.). String pool functions implemented in the HotSpot VM StringTable in a class, it is a hash table (add a hash learning), which kept the resident string (that is, we often say that the double quotes) references (instead of residing instance of the string itself), that is to say after some string instance heap by this reference is equivalent StringTable was given a "resident string" identity. This StringTable only one in each instance HotSpot VM, and is shared by all classes. 
example

String a = "abc";
String b = "abc";
System.out.println(a == b);
1
2
3
答案为true

A = String "abc";
System.out.println (A == "abc");
. 1
2
the answer is false 
Analysis: first instantiate "abc" is stored in the reference string and the constant pool JVM. 
The constant pool is divided into pools compile-time constants and runtime constant pool 
a == "abc" this code in the "abc" is the unassigned, jvm will optimize the operation String constants, string undeclared not at compile time constant pool, only if you are running only instances of the string to the runtime constant pool, and declared a compiler constant pool, it is false. Let's return to print (a == b) // true print this code may skip the JVM compile time, so, "abc" defined within the print run is obtained, comparison address, the result is output true.
 

Published 19 original articles · won praise 149 · views 800 000 +

Guess you like

Origin blog.csdn.net/truelove12358/article/details/101435038