<Part 1 of JVM: Memory and Garbage Collection>10 - StringTable

Source of notes: A full set of JVM tutorials in Shang Silicon Valley, millions of plays, the peak of the entire network (Song Hongkang explains the java virtual machine in detail)

10.1. Basic properties of String

  • String: a string, represented by a pair of "" quotes
  • String is declared final and cannot be inherited
  • String implements the Serializable interface: it means that the string supports serialization.
  • String implements the Comparable interface: it means that the string can be compared in size
  • String is defined internally in jdk8 and before final char[] valuefor storing string data. In JDK9, change tobyte[]

10.1.1. String storage structure changes in jdk9

Official website address: JEP 254: Compact Strings (java.net)

Motivation

The current implementation of the String class stores characters in a char array, using two bytes (sixteen bits) for each character. Data gathered from many different applications indicates that strings are a major component of heap usage and, moreover, that most String objects contain only Latin-1 characters. Such characters require only one byte of storage, hence half of the space in the internal char arrays of such String objects is going unused.

Description

We propose to change the internal representation of the String class from a UTF-16 char array to a byte array plus an encoding-flag field. The new String class will store characters encoded either as ISO-8859-1/Latin-1 (one byte per character), or as UTF-16 (two bytes per character), based upon the contents of the string. The encoding flag will indicate which encoding is used.

String-related classes such as AbstractStringBuilder, StringBuilder, and StringBuffer will be updated to use the same representation, as will the HotSpot VM’s intrinsic string operations.

This is purely an implementation change, with no changes to existing public interfaces. There are no plans to add any new public APIs or other interfaces.

The prototyping work done to date confirms the expected reduction in memory footprint, substantial reductions of GC activity, and minor performance regressions in some corner cases.

motivation

The current implementation of the String class stores characters in a char array, using two bytes (16 bits) per character. Data collected from many different applications shows that strings are a major component of heap usage, and in addition,Most string objects contain only Latin-1 characters. These characters require only one byte of storage, so half of the internal character arrays of these string objects are unused

illustrate

We propose to change the internal representation of the String class from an array of UTF-16 characters tobyte array plus encoding flags field. The new String class will store character encodings as ISO-8859-1/Latin-1 (one byte per character) or UTF-16 (two bytes per character), depending on the content of the string. The encoding flag will indicate which encoding is used.


Classes related to strings, such asAbstractStringBuilder, StringBuilder and StringBuffer will be updated to use the same representation, as will the intrinsic string operations of the HotSpot VM

This is purely an implementation change, no changes to the existing public interface. There are currently no plans to add any new public APIs or other interfaces.

The prototyping work done so far has confirmed the expected reduction in memory footprint, a large reduction in GC activity, and a slight performance regression in some corner cases.

Conclusion: String is no longer stored in char[], but changed to byte[] plus encoding mark, which saves some space

public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
    
    
    @Stable
    private final byte[] value;
}

10.1.2. Basic properties of String

String: Represents an immutable sequence of characters. Abbreviation: 不可变性.

  • When reassigning a string, it is necessary to rewrite the specified memory area for assignment, and the original value cannot be used for assignment.
  • When performing concatenation operations on existing strings, it is also necessary to re-specify the memory area for assignment, and the original value cannot be used for assignment.
  • When calling the replace() method of string to modify the specified character or string, it is also necessary to re-specify the memory area for assignment, and the original value cannot be used for assignment.

Assign a value to a string by means of a literal value (different from new), and the string value at this time is declared in the string constant pool

Case presentation:

/**
 * String的基本使用:体现String的不可变性
 *
 * @author shkstart  [email protected]
 * @create 2020  23:42
 */
public class StringTest1 {
    
    
    @Test
    public void test1() {
    
    
        String s1 = "abc";//字面量定义的方式,"abc"存储在字符串常量池中
        String s2 = "abc";
        s1 = "hello";

        System.out.println(s1 == s2);//判断地址:true  --> false

        System.out.println(s1);//
        System.out.println(s2);//abc

    }

    @Test
    public void test2() {
    
    
        String s1 = "abc";
        String s2 = "abc";
        s2 += "def";
        System.out.println(s2);//abcdef
        System.out.println(s1);//abc
    }

    @Test
    public void test3() {
    
    
        String s1 = "abc";
        String s2 = s1.replace('a', 'm');
        System.out.println(s1);//abc
        System.out.println(s2);//mbc
    }
}

The string constant pool will not store strings with the same content

The String Pool of String is a Hashtable with a fixed size , and the default size and length are 1009. If a lot of Strings are put into the String Pool, serious Hash conflicts will result, resulting in a very long linked list, and the direct impact of a long linked list is that the performance will drop significantly when calling String.intern.

Use -XX:StringTablesizecan set the length of StringTable

  • StringTable is fixed in jdk6, which is the length of 1009, so if there are too many strings in the constant pool, the efficiency will drop rapidly. StringTablesize setting is not required
  • In jdk7, the default value of the StringTable length is 60013, and there is no requirement for the StringTable size setting
  • In JDK8, if you set the length of StringTable, 1009 is the minimum value that can be set

**Note:** Why does the performance drop significantly when the linked list is long when String.intern is called?

The bottom layer of the String Pool is a HashTable, calling String.intern() is to add a string to the string constant pool. Before joining, it will first judge whether the string exists in the pool. If the linked list is too long in this process, the speed of traversing the string will be very low and its performance will be reduced.

Why JDK6 --> JDK7, the default size of StringTable increased?

The larger the StringTable, the larger the length of the underlying array corresponding to the HashTable, the smaller the probability of Hash conflicts when adding elements, the shorter the length of the linked list, the shorter the time to traverse to find elements, and the higher the efficiency! (It takes 143ms to add 10W strings when the length is 1009. When the length is 60013, it takes 47ms)

Test code:

/**
 * 产生10万个长度不超过10的字符串,包含a-z,A-Z
 * @author shkstart  [email protected]
 * @create 2020  23:58
 */
public class GenerateString {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileWriter fw =  new FileWriter("words.txt");

        for (int i = 0; i < 100000; i++) {
    
    
            //1 - 10
           int length = (int)(Math.random() * (10 - 1 + 1) + 1);
            fw.write(getString(length) + "\n");
        }

        fw.close();
    }

    public static String getString(int length){
    
    
        String str = "";
        for (int i = 0; i < length; i++) {
    
    
            //65 - 90, 97-122
            int num = (int)(Math.random() * (90 - 65 + 1) + 65) + (int)(Math.random() * 2) * 32;
            str += (char)num;
        }
        return str;
    }
}

/**
 * 测试在StringTableSize=1009 和 StringTableSize=60013下,添加10W个字符串耗时情况
 * 结论:1009:143ms  100009:47ms
 *  配置:-XX:StringTableSize=1009
 * @author shkstart  [email protected]
 * @create 2020  23:53
 */
public class StringTest2 {
    
    
    public static void main(String[] args) {
    
    
        //测试StringTableSize参数
//        System.out.println("我来打个酱油");
//        try {
    
    
//            Thread.sleep(1000000);
//        } catch (InterruptedException e) {
    
    
//            e.printStackTrace();
//        }

        BufferedReader br = null;
        try {
    
    
            br = new BufferedReader(new FileReader("words.txt"));
            long start = System.currentTimeMillis();
            String data;
            while((data = br.readLine()) != null){
    
    
                data.intern(); //如果字符串常量池中没有对应data的字符串的话,则在常量池中生成
            }

            long end = System.currentTimeMillis();
            // 数组越长,Hash碰撞越少,效率越高~
            System.out.println("花费的时间为:" + (end - start));//1009:143ms  100009:47ms
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if(br != null){
    
    
                try {
    
    
                    br.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }

            }
        }
    }
}

10.2. String memory allocation

There are 8 basic data types and a special type String in the Java language. In order to make them faster and save memory during operation, these types provide a concept of constant pool.

The constant pool is similar to a cache provided by the Java system level. The constant pools of the 8 basic data types are coordinated by the system,The constant pool of String type is special. There are two main ways to use it.

  • String objects declared directly using double quotes will be directly stored in the constant pool.

  • If it is not a String object declared with double quotes, you can use the intern() method provided by String. This will be discussed later

Java 6 and before, the string constant pool is stored in the permanent generation

In Java 7, Oracle engineers made a big change to the logic of the string pool, which will beThe location of the string constant pool is adjusted to the Java heap

  • All strings are saved in the heap (Heap), just like other ordinary objects, so that you only need to adjust the heap size when tuning the application.

  • The concept of string constant pool was originally used more, but this change gives us enough reasons for us to reconsider using it in Java 7 String.intern().

Java8 metaspace, string constants are on the heap

image-20200711093546398

image-20200711093558709

For a more detailed description, please refer to: https://blog.csdn.net/qq_43842093/article/details/122991756

Why should StringTable be adjusted?

Official website address: Java SE 7 Features and Enhancements (oracle.com)

Synopsis: In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

Summary: In JDK 7, internal strings are no longer allocated in the permanent generation of the Java heap, but in major parts of the Java heap (called the young and old generation), along with other objects created by the application. This change will result in more data residing in the main Java heap and less data in the permanent generation, so the heap may need to be resized. Most applications will see a relatively small difference in heap usage due to this change, butLarger applications that load many classes or make heavy use of the String.intern() method will see a more pronounced difference

Reasons for adjustment: ①permSize is relatively small by default ②The frequency of permanent generation garbage collection is low, so OOM is more likely to occur

10.3. Basic operations on String

/**
 * Debug查看字符串常量池中有多少字符串
 * @author shkstart  [email protected]
 * @create 2020  0:49
 */
@Test
public void test1() {
    
    
    System.out.print1n("1"); //2321
    System.out.println("2");
    System.out.println("3");
    System.out.println("4");
    System.out.println("5");
    System.out.println("6");
    System.out.println("7");
    System.out.println("8");
    System.out.println("9");
    System.out.println("10"); //2330
    System.out.println("1"); //2330 因为池中已经存在该字符串了,所以不会再创建
    System.out.println("2"); //2330
    System.out.println("3");
    System.out.println("4");
    System.out.println("5");
    System.out.print1n("6");
    System.out.print1n("7");
    System.out.println("8");
    System.out.println("9");
    System.out.println("10");//2330
}

The Java language specification requires that identical string literals should contain the same Unicode character sequence (constants containing the same code point sequence), and must point to the same instance of the String class.

class Memory {
    
    
    public static void main(String[] args) {
    
    //line 1
        int i= 1;//line 2
        Object obj = new Object();//line 3
        Memory mem = new Memory();//Line 4
        mem.foo(obj);//Line 5
    }//Line 9
    private void foo(Object param) {
    
    //line 6
        String str = param.toString();//line 7
        System.out.println(str);
    }//Line 8
}

image-20210511111607132

Note: Only the String and literal values ​​of intern() will be added to the string,Object#toString returns the result of splicing multiple characters, not the former

In addition new String(), the form will be explained later

10.4. String concatenation operation

  • The splicing result of constants and constants is in the constant pool, the principle is compile-time optimization
  • Variables with the same content will not exist in the constant pool
  • As long as one of them is a variable, the result is in the heap [the heap here refers to other heap areas other than the string constant pool]. The principle of variable splicing is StringBuilder
  • If the splicing result calls the intern() method, actively put the string object that is not in the constant pool into the pool, and return the address of this object

Example 1

@Test
public void test1(){
    
    
    String s1 = "a" + "b" + "c";//编译期优化:等同于"abc"
    String s2 = "abc"; //"abc"一定是放在字符串常量池中,将此地址赋给s2
    /*
         * 最终.java编译成.class,再执行.class
         * String s1 = "abc";
         * String s2 = "abc"
         */
    System.out.println(s1 == s2); //true
    System.out.println(s1.equals(s2)); //true
}

Example 2

@Test
public void test2(){
    
    
    String s1 = "javaEE";
    String s2 = "hadoop";

    String s3 = "javaEEhadoop";
    String s4 = "javaEE" + "hadoop";//编译期优化
    //如果拼接符号的前后出现了变量,则相当于在堆空间中new String(),具体的内容为拼接的结果:javaEEhadoop
    String s5 = s1 + "hadoop";
    String s6 = "javaEE" + s2;
    String s7 = s1 + s2;

    System.out.println(s3 == s4);//true
    System.out.println(s3 == s5);//false
    System.out.println(s3 == s6);//false
    System.out.println(s3 == s7);//false
    System.out.println(s5 == s6);//false
    System.out.println(s5 == s7);//false
    System.out.println(s6 == s7);//false
    //intern():判断字符串常量池中是否存在javaEEhadoop值,如果存在,则返回常量池中javaEEhadoop的地址;
    //如果字符串常量池中不存在javaEEhadoop,则在常量池中加载一份javaEEhadoop,并返回次对象的地址。
    String s8 = s6.intern();
    System.out.println(s3 == s8);//true
}

Example 3

@Test
public void test3(){
    
    
    String s1 = "a";
    String s2 = "b";
    String s3 = "ab";
    /*
        如下的s1 + s2 的执行细节:(变量s是我临时定义的)
        ① StringBuilder s = new StringBuilder();
        ② s.append("a")
        ③ s.append("b")
        ④ s.toString()  --> 约等于 new String("ab")

        补充:在jdk5.0之后使用的是StringBuilder,在jdk5.0之前使用的是StringBuffer
         */
    String s4 = s1 + s2;//
    System.out.println(s3 == s4);//false
}

Example 4

  • Without final modification, it is a variable. For example, s1 and s2 in line s3 will be spliced ​​by new StringBuilder
  • Use final modification, which is a constant. Code optimization will be performed by the compiler.In actual development, if you can use final, try to use
    /*
    1. 字符串拼接操作不一定使用的是StringBuilder!
       如果拼接符号左右两边都是字符串常量或常量引用,则仍然使用编译期优化,即非StringBuilder的方式。
    2. 针对于final修饰类、方法、基本数据类型、引用数据类型的量的结构时,能使用上final的时候建议使用上。
       final修饰的,在编译时就初始化好了
     */
@Test
public void test4(){
    
    
    final String s1 = "a";
    final String s2 = "b";
    String s3 = "ab";
    String s4 = s1 + s2;
    System.out.println(s3 == s4);//true
}

Bytecode Perspective Analysis

We take a look at the bytecode of Example 3, we can find that s1 + s2a StringBuilder object is actually new, and use the append method to add s1 and s2, and finally call the toString method to assign it to s4

 0 ldc #14 <a> #加载常量a
 2 astore_1       #将a放到局部变量表的下标为1的位置
 3 ldc #15 <b>
 5 astore_2       #将b放到局部变量表的下标为2的位置
 6 ldc #16 <ab>
 8 astore_3
 9 new #9 <java/lang/StringBuilder> #创建StringBuilder对象
12 dup
13 invokespecial #10 <java/lang/StringBuilder.<init> : ()V>
16 aload_1
17 invokevirtual #11 <java/lang/StringBuilder.append : (Ljava/lang/String;)Ljava/lang/StringBuilder;> #append
20 aload_2
21 invokevirtual #11 <java/lang/StringBuilder.append : (Ljava/lang/String;)Ljava/lang/StringBuilder;> #append
24 invokevirtual #12 <java/lang/StringBuilder.toString : ()Ljava/lang/String;> #toString
27 astore 4
29 getstatic #3 <java/lang/System.out : Ljava/io/PrintStream;>
32 aload_3
33 aload 4
35 if_acmpne 42 (+7)
38 iconst_1
39 goto 43 (+4)
42 iconst_0
43 invokevirtual #4 <java/io/PrintStream.println : (Z)V>
46 return

String concatenation operation performance comparison

    /*
    体会执行效率:通过StringBuilder的append()的方式添加字符串的效率要远高于使用String的字符串拼接方式!
    
    详情:① StringBuilder的append()的方式:自始至终中只创建过一个StringBuilder的对象
          	        使用String的字符串拼接方式:创建过多个StringBuilder和String的对象
         	② 使用String的字符串拼接方式:内存中由于创建了较多的StringBuilder和String的对象,内存占用更大;如果进行GC,需要花费额外的时间。

     改进的空间:在实际开发中,如果基本确定要前前后后添加的字符串长度不高于某个限定值highLevel的情况下,建议使用构造器实例化:
               StringBuilder s = new StringBuilder(highLevel);//new char[highLevel]
     */
@Test
public void test6(){
    
    

    long start = System.currentTimeMillis();

    //        method1(100000);//4014
    method2(100000);//7

    long end = System.currentTimeMillis();

    System.out.println("花费的时间为:" + (end - start));
}

public void method1(int highLevel){
    
    
    String src = "";
    for(int i = 0;i < highLevel;i++){
    
    
        src = src + "a";//每次循环都会创建一个StringBuilder、String
    }
}

public void method2(int highLevel){
    
    
    //只需要创建一个StringBuilder
    StringBuilder src = new StringBuilder();
    src.toString();
    for (int i = 0; i < highLevel; i++) {
    
    
        src.append("a");
    }
}      

10.5. Use of intern()

10.5.1 Basic introduction of intern()

Explanation in the official API docs

public String intern()

Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java™ Language Specification.

  • Returns:

    a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

When the intern method is called, if the pool already contains a string equal to this String object, as determined by the equals(Object) method, then the string in the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

Thus, for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and constant expressions taking string values ​​are interned.

Returns a string with the same content as this string, but guaranteed to be from a unique pool of strings.


intern is a native method that calls the underlying C method

public native String intern();

If it is not a String object declared with double quotes, you can use the intern method provided by String, which will query whether the current string exists from the string constant pool, and if not, put the current string into the constant pool.

String myInfo = new string("I love atguigu").intern();

That is to say, if the String.intern method is called on any string, the class instance pointed to by the returned result must be exactly the same as the string instance that appears directly in the form of a constant. Therefore, the following expressions must evaluate to true

("a"+"b"+"c").intern() == "abc"

In layman's terms, Interned string is to ensure that there is only one copy of the string in memory, which can save memory space and speed up the execution of string manipulation tasks. Note that this value will be stored in the String Intern Pool (String Intern Pool)

image-20210511145542579

How to ensure that the variable s points to the data in the string constant pool?

There are two ways:

  • Method 1: String s = "shkstart";//The method of literal definition

  • Method 2: Call intern()

    String s = new String(“shkstart”).intern();

    String s = new StringBuilder(“shkstart”).toString().intern();

10.5.2 Two interview questions about String

Interview question: How many objects will new String("ab") create? What about new String("a") + new String("b")?

String str = new String("ab");

By looking at the bytecode, you know there are two.

  • 一个对象是:new关键字在堆空间创建的
    
  • 另一个对象是:字符串常量池中的对象"ab"。 字节码指令ldc :在常量池中常见对象
    

image-20230215152940742

Why should it be designed as two?

Then when you use literal parameters, there must always be a place for this parameter. It is a literal value, so it is naturally placed in the string constant pool
and then the new String operation uses this parameter to assign a value to the new object in the heap space. , and then give the new object address to str

String str = new String("a") + new String("b");

10.5.3 Use of intern: JDK6 vs JDK7/8

/**
 * 看一道关于intern的面试题
 */
public class StringIntern {
    
    
    public static void main(String[] args) {
    
    

        String s = new String("1");// s记录的地址:堆空间中new String("1")的地址
        s.intern();//调用此方法之前,字符串常量池中已经存在了"1"
        String s2 = "1";// s2记录的地址:常量池中对象的地址
        System.out.println(s == s2);//jdk6:false   jdk7/8:false


        String s3 = new String("1") + new String("1");//s3变量记录的地址为:new String("11")
        //执行完上一行代码以后,字符串常量池中,是否存在"11"呢?答案:不存在!!
        s3.intern();//在字符串常量池中生成"11"。如何理解:jdk6:创建了一个新的对象"11",也就有新的地址。
                                            //         jdk7:此时常量中并没有创建"11",而是创建一个指向堆空间中new String("11")的地址
        		            // 本来该生成两个地址,但是都是在堆空间,而且存11,那就没必要再开辟串池空间了。这是JDK的优化
       
        String s4 = "11";//s4变量记录的地址:使用的是上一行代码代码执行时,在常量池中生成的"11"的地址
        System.out.println(s3 == s4);//jdk6:false  jdk7/8:true
    }

}

diagram

jdk6 diagram

jdk7 Figure 1

To summarize the use of String's intern():

In JDK1.6, try to put this string object into the string pool.

  • If there is one in the string pool, it will not be put in. Returns the address of an object in an existing string pool
  • If not, put thisobject copy, into the string pool, and return the address of the object in the string pool

Starting from JDK1.7, try to put this string object into the string pool.

  • If there is one in the string pool, it will not be put in. Returns the address of an object in an existing string pool
  • If not, it will putobject reference addressMake a copy, put it into the string pool, and return the reference address in the string pool

Reference article: In-depth analysis of String#intern by Meituan technical team

10.5.4 Exercises on intern()

exercise 1

/**
 * @author shkstart  [email protected]
 * @create 2020  20:17
 */
public class StringExer1 {
    
    
    public static void main(String[] args) {
    
    
        // String x = "ab";
        String s = new String("a") + new String("b");//new String("ab")
        //在上一行代码执行完以后,字符串常量池中并没有"ab"

        String s2 = s.intern();//jdk6中:在串池中创建一个字符串"ab"
                               //jdk8中:串池中没有创建字符串"ab",而是创建一个引用,指向new String("ab"),将此引用返回

        System.out.println(s2 == "ab");//jdk6:true  jdk8:true
        System.out.println(s == "ab");//jdk6:false  jdk8:true
    }
}

Illustration:

image-20200711150859709

image-20200711151326909

image-20200711151433277

exercise 2

/**
 *
 * 10行:false
 * 11行:jdk6:false jdk7/8:true 
 * @author shkstart  [email protected]
 * @create 2020  20:26
 */
public class StringExer2 {
    
    
    public static void main(String[] args) {
    
    
        String s1 = new String("ab");//执行完以后,会在字符串常量池中会生成"ab"
//        String s1 = new String("a") + new String("b");执行完以后,不会在字符串常量池中会生成"ab"
        s1.intern();
        String s2 = "ab";
        System.out.println(s1 == s2);
    }
}

10.5.5 Efficiency Tests for Interns: Spatial Angles

Let's test it. There is actually quite a difference between using intern and not using it.

/**
 * 使用intern()测试执行效率:空间使用上
  *
 *
 * @author shkstart  [email protected]
 * @create 2020  21:17
 */
public class   StringIntern2 {
    
    
    static final int MAX_COUNT = 1000 * 10000;
    static final String[] arr = new String[MAX_COUNT];

    public static void main(String[] args) {
    
    
        Integer[] data = new Integer[]{
    
    1,2,3,4,5,6,7,8,9,10};

        long start = System.currentTimeMillis();
        for (int i = 0; i < MAX_COUNT; i++) {
    
    
//            arr[i] = new String(String.valueOf(data[i % data.length]));
            arr[i] = new String(String.valueOf(data[i % data.length])).intern();

        }
        long end = System.currentTimeMillis();
        System.out.println("花费的时间为:" + (end - start));

        try {
    
    
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.gc();
    }
}

Use Jprofiler or JVisualVM to observe the creation of String objects:

Conclusion : When a large number of existing strings are used in the program, especially when there are many repeated strings, using the intern() method can save memory space.

Analysis: Why can the memory be reduced?

Calling intern() will cause the object in the constant pool to be used each time. Although it is also created in the heap space, we use the one in the constant pool. The part in the heap space will be GCed because no one uses it, so that the memory can be reduced.

A large website platform needs to store a large number of strings in memory. For example, social networking sites, many people store: Beijing, Haidian District and other information. At this time, if the string calls the intern() method, the memory size will be significantly reduced.

10.6. Garbage Collection of StringTable

/**
 * String的垃圾回收:
 * -Xms15m -Xmx15m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails
 *
 * @author shkstart  [email protected]
 * @create 2020  21:27
 */
public class StringGCTest {
    
    
    public static void main(String[] args) {
    
    
        for (int j = 0; j < 100000; j++) {
    
    
            String.valueOf(j).intern(); //通过源码可知 等价于 new String(..).intern()
        }
    }
}

operation result:

image-20230215210718685

10.7. String deduplication in G1

Official website address: JEP 192: String Deduplication in G1 (java.net)

Motivation

Many large-scale Java applications are currently bottlenecked on memory. Measurements have shown that roughly 25% of the Java heap live data set in these types of applications is consumed by String objects. Further, roughly half of those String objects are duplicates, where duplicates means string1.equals(string2) is true. Having duplicate String objects on the heap is, essentially, just a waste of memory. This project will implement automatic and continuous String deduplication in the G1 garbage collector to avoid wasting memory and reduce the memory footprint.

Currently, many large-scale Java applications are bottlenecked on memory. Measurements show that in these types of applications, about 25% of the Java heap real-time data set is occupied by String'对象所消耗。此外,这些 "String "对象中大约有一半是重复的,其中重复意味着 "string1.equals(string2) "是真的。在堆上有重复的String' objects, which are essentially just a waste of memory. This project will implement automatic and continuous `String' deduplication in G1 garbage collector to avoid wasting memory and reduce memory footprint.


Note that the duplication mentioned here refers to the data in the heap, not in the constant pool, because the constant pool itself will not be repeated

Background: Testing with many Java applications (both large and small) yielded the following results:

  • String objects account for 25% of the heap survival data collection
  • There are 13.5% of repeated string objects in the heap survival data collection
  • The average length of a string object is 45

The bottleneck of many large-scale Java applications is memory, and tests have shown that in these types of applications,Almost 25% of the data collections surviving in the Java heap are String objects. Furthermore, almost half of the string objects are repeated, and the meaning of repetition is: stringl.equals(string2)= true.Duplicate String objects on the heap must be a waste of memory. This project will implement automatic and continuous deduplication of duplicate string objects in the G1 garbage collector, so as to avoid wasting memory.

accomplish

  1. When the garbage collector is working, it will access the live objects on the heap.For each accessed object, check whether it is a candidate String object to be deduplicated
  2. If so, insert a reference to this object into the queue for further processing. A deduplication thread runs in the background, processing this queue. Processing an element of the queue means removing the element from the queue and then attempting to dereference the string object it refers to.
  3. Use a hashtable to record all unique char arrays used by String objects. When removing duplicates, the hashtable will be checked to see if an identical char array already exists on the heap.
  4. If it exists, the String object will be adjusted to refer to that array, releasing the reference to the original array, which will eventually be reclaimed by the garbage collector.
  5. If the lookup fails, the char array is inserted into the hashtable so that the array can be shared later.

command line options

  • UseStringDeduplication(bool): Enable String deduplication, which is not enabled by default and needs to be enabled manually.
  • PrintStringDeduplicationStatistics(bool) : Print detailed deduplication statistics
  • StringpeDuplicationAgeThreshold(uintx): String objects reaching this age are considered candidates for deduplication

Guess you like

Origin blog.csdn.net/LXYDSF/article/details/129052698