当前位置:网站首页>Class constant pool and runtime constant pool

Class constant pool and runtime constant pool

2022-07-07 03:58:00 The season when the monsoon dies

class Constant pool

     java The file is being compiled into class After the document , stay class A constant pool will be generated in the file , The literal amount used to store code 、 Symbol reference , Such as static、void、public wait . This constant pool is called class Constant pool . use javap The command generates a readable JVM Bytecode instruction file :

javap -v ScheduledBlockChainTask.class

  The red box indicates class Constant pool information , There are two main types of constants in the constant pool : Literal and symbolic references .

Literal
Literal quantity refers to the letters 、 A string of numbers or numeric constants
Literal can only appear with right value , The so-called right value refers to the value on the right of the equal sign , Such as :int a=1 there a Is the left value ,1 Is the right value . In this case 1 It's literal
int a = 1; 
int b = 2; 
int c = "abcdefg"; 
int d = "abcdefg
Symbol reference
Symbolic reference is a concept in compilation principle , It's relative to direct reference . It mainly includes three kinds of constants :
  • Fully qualified names of classes and interfaces
  • Name and descriptor of the field
  • The name and descriptor of the method
above a,b That's the field name , It's a kind of symbolic reference , also Math Class constant pool Lcom/tuling/jvm/Math Is the fully qualified name of the class ,main and compute It's the method name ,() It's a kind of UTF8 Format descriptor , These are all symbolic references .
These constant pools are now static information , Only when the runtime is loaded into memory , These symbols have the corresponding memory address information , Once these constant pools are loaded into memory, they become Runtime constant pool , The corresponding symbol reference will be transformed into the direct reference of the code loaded into the memory area when the program is loaded or running , That's what we're talking about Dynamically linked . for example ,compute() This symbolic reference is converted to at run time compute() Method specific code in memory Address , Mainly through the type pointer in the object header to convert the direct reference .
String constant pool
The design idea of string constant pool
1. String assignment , Like any other object assignment , It costs a lot of time and space , As the most basic data type , A lot of frequent string creation , Greatly affect the performance of the program
2. JVM To improve performance and reduce memory overhead , There are some optimizations when instantiating string constants
  • Create a string constant pool for Strings , Similar to the cache
  • When creating a string constant , First, query whether the string constant pool exists
  • The string exists , Return reference instance , non-existent , Instantiate the string and put it in the pool

String constant pool location
  • Jdk1.6 And before : There is a permanent generation , The runtime constant pool is in the permanent generation , Runtime constant pool contains string constant pool
  • Jdk1.7: There is a permanent generation , But gradually “ To the eternal generation ”, The string constant pool is separated from the runtime constant pool in the permanent generation into the heap
  • Jdk1.8 And after : There is no permanent generation , Runtime constant pool in meta space , The string constant pool is still in the heap

Three string operations

  • Assign strings directly
 String s = "lamu"; // s Point to references in the constant pool 
String objects created in this way , Only in the constant pool .
Because there is "lamu" This literal quantity , Create objects s When ,JVM I'll go to the constant pool first equals(key) Method , Judge whether there are the same objects ; If there is , Returns the reference of the object in the constant pool directly ; without , A new object is created in the constant pool , And back to the reference .
  • new String();
String s1 = new String("zhuge"); // s1 Point to an object reference in memory 
This method ensures that the object exists in both the string constant pool and the heap , Create without , Finally, the object reference in heap memory is returned .
The steps are roughly as follows :
    Because there is "lamu" This literal quantity , So we will first check whether there is a string in the string constant pool "lamu"
    non-existent , First, create a string object in the string constant pool ; Then go to memory and create a string object "lamu";
    exist , Just go to the heap memory and create a string object "lamu";
Last , Returns a reference in memory to .
  • intern Method
String s1 = new String("zhuge"); 
String s2 = s1.intern(); 
System.out.println(s1 == s2); //false

   intern Method returns the reference of the object in the string constant pool . stay jdk6 And below , If the object exists in the string constant pool , Returns the reference of the object in the string constant pool . without , Then add the object to the string constant pool , Returns the reference of this object in the string constant pool . stay jdk6 In the previous version , If the object exists in the string constant pool , Returns the reference of the object in the string constant pool . without , Then check whether the object exists in the heap , If there is , Put the reference of this object in the heap into the string constant pool and return .

String Splicer “+” What will the bottom do

If at compile time , It can determine the value of objects before and after splicing , Then it will be spliced directly in the compilation stage , Put the final value into the string constant pool . If you can't know the value of the object before and after splicing at the compilation stage , Then an object will be created in the heap and string constant pool by splicing the front and rear field values at run time .

String s1 = new String("he") + new String("llo"); 
String s2 = s1.intern(); 
System.out.println(s1 == s2); 
//  stay  JDK 1.6  Lower output yes  false, Created  6  Objects  
//  stay  JDK 1.7  And above version output is  true, Created  5  Objects  
//  Of course, we didn't consider GC, But these objects do exist or exist 
//  Be careful : Whether the constant pool has a corresponding object depends on the literal , Objects with non-existent literals will not appear in the constant pool 


String s0="hello"; 
String s1="hello"; 
String s2="he" + "llo"; 
System.out.println( s0==s1 ); //true 
System.out.println( s0==s2 ); //true  s2 It will be optimized to "hello".


String s0="hello"; 
String s1=new String("hello"); 
String s2="he" + new String("llo"); 
System.out.println( s0==s1 ); // false   s0 In the constant pool ,s1 In the heap 
System.out.println( s0==s2 ); // false  new String("llo") Cannot optimize at compile time , therefore s2 Finally, it is generated in the heap 
System.out.println( s1==s2 ); // false   s1 and s2 It's all in the pile , But they are new Two different objects come out 


String a = "ab"; 
String bb = "b"; 
String b = "a" + bb;
System.out.println(a == b); // false   Variable bb Unknown at compile time , Can't optimize 


String a = "ab"; 
final String bb = "b"; 
String b = "a" + bb; 
System.out.println(a == b); // true  bb use final modification , The compile time is known ,b It can be optimized to "ab"


String a = "ab"; 
final String bb = getBB(); 
String b = "a" + bb; 
System.out.println(a == b); // false  Method is used final modification , It also needs to generate dynamic link execution code at runtime , Therefore, the compilation time is unknown , Can't optimize 
private static String getBB() { 
 return "b";
}



String str2 = new StringBuilder(" Computer ").append(" technology ").toString(); 
System.out.println(str2 == str2.intern()); //true
// str2 Equal to the objects in the heap " Computer technology ", Because there is no literal " Computer technology ", So this object does not exist in the constant pool .
// str2.intern() The reference to the heap object in the returned string constant pool , So it's the same object 



String str1 = new StringBuilder("ja").append("va").toString(); 
System.out.println(str1 == str1.intern()); //false 
// java Is the key word , stay JVM The initialized related classes are put into the string constant pool .


String s1=new String("test"); 
System.out.println(s1==s1.intern()); //false
// new The object that comes out already exists in the constant pool ,s1.intern() Returns an object from a constant pool .


Eight basic types of wrapper classes and object pools
    java Most of the basic types of wrapper classes in have implemented constant pool technology ( Strictly speaking, it should be called Object pool , On the pile ), These classes are Byte,Short,Integer,Long,Character,Boolean, The other two floating-point type wrapper classes are not implemented . in addition
Byte,Short,Integer,Long,Character this 5 The packing class of an integer is only when the corresponding value is less than or equal to 127 The object pool can be used only when the object pool is used , That is, objects are not responsible for creating and managing 127 Objects of these classes . Because the probability of using this relatively small number is relatively large .
//5 Plastic packaging Byte,Short,Integer,Long,Character The object of , 
// When the value is less than 127 You can use the object pool  
Integer i1 = 127; // The underlying layer of this call is actually executed Integer.valueOf(127), It uses IntegerCache Object pool  
Integer i2 = 127; 
System.out.println(i1 == i2);// Output true


// Greater than 127 when , No objects are taken from the object pool  
Integer i3 = 128; 
Integer i4 = 128; 
System.out.println(i3 == i4);// Output false


// use new Keywords newly generated objects do not use object pools  
Integer i5 = new Integer(127); 
Integer i6 = new Integer(127); 
Integer i7 = 127; 
System.out.println(i5 == i6);// Output false
System.out.println(i5 == i7);// Output false


//Boolean Class also implements object pooling Technology  
Boolean bool1 = true; 
Boolean bool2 = true; 
System.out.println(bool1 == bool2);// Output true 
// Floating point wrapper classes don't implement object pooling  
Double d1 = 1.0; 
Double d2 = 1.0; 
System.out.println(d1 == d2);// Output false

原网站

版权声明
本文为[The season when the monsoon dies]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062100174166.html