jvm-07字符串常量池StringTable

1.String的基本特性

  • String:字符串,使用一对""引起来表示。
    • String sl = “hello”;//字面量的定义方式
    • String s2 = new String(“hello”) ;
  • String声明为final的, 不可被继承
  • String实现了Serializable接口:表示字符串是支持序列化的。 实现了Comparable接口:表示String可以比较大小
  • String在jdk8及以前内部定义了final char[],value用于存储字符串数据。jdk9时改为final byte[]
    • 结论: JDK9之后String再也不用char[] 来存储啦,改成了byte[] 加上编码标记(中文是两个字节,英文是了一个字节),节约了一些空间。StringBuffer和StringBuilder也做了一些修改
public final class String implements java.io.Serializable, Comparable<String>,CharSequence {
@Stable
private final byte[] value;
}
  • String:代表不可变的字符序列。简称:不可变性。
    • 当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值。
    • 当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
    • 当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
  • 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
/**
 * String的基本使用:体现String的不可变性
 */
public class StringTest1 {
    @Test
    public void test1() {
        String s1 = "abc";//字面量定义的方式,"abc"存储在字符串常量池中
        String s2 = "abc";//s1,s2两个指向一样的内容


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

		s1 = "hello";
		System.out.println(s1 == s2);//判断地址: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
    }
}
String str = new String("good");
char[] ch = {'t', 'e', 's', 't'};

public void change(String str, char ch[]) {
    str = "test ok";  //String形参不能变
    //this.str = "test ok";   结果就变成了test ok
    ch[0] = 'b';//char数组引用可以改变
}

public static void main(String[] args) {
    StringExer ex = new StringExer();
    ex.change(ex.str, ex.ch);
    System.out.println(ex.str);//good
    System.out.println(ex.ch);//best
}
  • 字符串常量池中是不会存储相同内容的字符串的。
    • String的String Pool 是一个固定大小的Hashtable,默认值大小长度是1009。如果放进StringPool的String非常多, 就会造成Hash冲突严重,从而导致链表会很长,而链表长了后直接会造成的影响就是当调用String. intern时性能会大幅下降。(合理设置长度可以增加性能)
    • 使用-XX:StringTableSize=xxx 可设置StringTable的长度(数组加链表)
    • 在jdk6中StringTable是固定的,默认就是1009的长度,所以如果常量池中的字符串过多就会导致效率下降很快。StringTableSize设置没有要求
    • 在jdk7中,StringTable的长度默认值是60013,可以随意设置
    • jdk8开始,默认大小60013,1009是StringTable长度可设置的最小值

2.String的内存分配

  • 在Java语言中有8种基本数据类型和一种比较特殊的类型String。这些类型为了使它们在运行过程中速度更快、更节省内存,都提供了一种常量池的概念。
  • 常量池就类似一个Java系统级别提供的缓存。8种基本数据类型的常量 池都是系统协调的,String类 型的常量池比较特殊。它的主要使用方法有两种。
    • 直接使用双引号声明出来的String对象会直接存储在常量池中。
      • 比如: String info = “a” ;
    • 如果不是用双引号声明的String对象,可以使用String提供的intern()方法。这个后面重点谈
  • Java 6及以前,字符串常量池存放在永久代。
  • Java 7中Oracle的工程师对字符串池的逻辑做了很大的改变,即将字符串常量池的位置调整到Java堆内。
    • 所有的字符串都保存在堆(Heap)中,和其他普通对象一样,这样可以让你在进行调优应用时仅需要调整堆大小就可以了。
    • 字符串常量池概念原本使用得比较多,但是这个改动使得我们有足够的理由让我们重新考虑在Java 7中使用String. intern()。
  • Java8元空间,字符串常量在堆

StringTable为什么要调整
①永久代permSize默认比较小;
②永久代的垃圾回收频率低;

3.String的基本操作

2321代表程序里的String个数 下面的字符串"1" 到 "10"不会再次加载

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 一个字符串被创建 放在堆空间 str放在堆里
        System.out.println(str);
    }//line 8
}

4.字符串拼接操作

  • 1.常量与常量的拼接结果在常量池,原理是编译期优化
  • 2.常量池中不会存在相同内容的常量。
  • 3.只要其中有一个是变量,结果就在堆中。变量拼接的原理是StringBuilder
  • 4.如果拼接的结果调用intern()方法,则主动将常量池中还没有的字符串对象放入池中,并返回此对象地址。
 @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
    }
 @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   不同的new的对象 地址不同
        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
    }
复制代码

字符串拼接

@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
    }
    

特殊情况

    /*
    1. 字符串拼接操作不一定使用的是StringBuilder!
       如果拼接符号左右两边都是字符串常量或常量引用,则仍然使用编译期优化,即非StringBuilder的方式。
    2. 针对于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
    }
    //练习:
    @Test
    public void test5(){
        String s1 = "javaEEhadoop";
        String s2 = "javaEE";
        String s3 = s2 + "hadoop";
        System.out.println(s1 == s3);//false

        final String s4 = "javaEE";//s4:常量
        String s5 = s4 + "hadoop";   //常量+常量
        System.out.println(s1 == s5);//true
  
    }

拼接操作与append的效率对比

append效率要比字符串拼接高很多

/*
    体会执行效率:通过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
        }
//        System.out.println(src);

    }

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

5.intern()的使用

https://tech.meituan.com/2014/03/06/in-depth-understanding-string-intern.html 有关于intern()的解释 很清楚

/** 
 * Returns a canonical representation for the string object. 
 * <p> 
 * A pool of strings, initially empty, is maintained privately by the 
 * class <code>String</code>. 
 * <p> 
 * When the intern method is invoked, if the pool already contains a 
 * string equal to this <code>String</code> object as determined by 
 * the {@link #equals(Object)} method, then the string from the pool is 
 * returned. Otherwise, this <code>String</code> object is added to the 
 * pool and a reference to this <code>String</code> object is returned. 
 * <p> 
 * It follows that for any two strings <code>s</code> and <code>t</code>, 
 * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code> 
 * if and only if <code>s.equals(t)</code> is <code>true</code>. 
 * <p> 
 * All literal strings and string-valued constant expressions are 
 * interned. String literals are defined in section 3.10.5 of the 
 * <cite>The Java&trade; Language Specification</cite>. 
 * 
 * @return  a string that has the same contents as this string, but is 
 *          guaranteed to be from a pool of unique strings. 
 */  
public native String intern();  

String#intern方法中看到,这个方法是一个 native 的方法,但注释写的非常明了。“如果常量池中存在当前字符串, 就会直接返回当前字符串. 如果常量池中没有此字符串, 会将此字符串放入常量池中后, 再返回”。

如果不是用双引号声明的String对象,可以使用String提供的intern方法: intern方法会从字符串常量池中查询当前字符串是否存在(equals()进行比较),若不存在就会将当前字符串放入常量池中。

  • 比如: String myInfo = new String(“I love u”).intern();
    也就是说,如果在任意字符串上调用String. intern方法,那么其返回结果所指向的那个类实例,必须和直接以常量形式出现的字符串实例完全相同。 因此,下 列表达式的值必定是true: (“a” + “b” + “c”).intern()== “abc”;
    通俗点讲,Interned String就是确保字符串在内存里只有一份拷贝,这样可以节约内存空间,加快字符串操作任务的执行速度。注意,这个值会被存放在字符串内部池(String Intern Pool)。

  • 如何保证变量s指向的是字符串常量池中的数据呢

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

    new String(“ab”)会创建几个对象, new String(“a”)+new String(“b”)呢

public class StringNewTest {
    public static void main(String[] args) {
//        String str = new String("ab");

        String str = new String("a") + new String("b");
    }
}
复制代码
  • new String(“ab”)会创建几个对象?看字节码,就知道是两个。
    • 一个对象是:new关键字在堆空间创建的
    • 另一个对象是:字符串常量池中的对象"ab"。 字节码指令:ldc
  • new String(“a”) + new String(“b”)呢?
    • 对象1:new StringBuilder()
    • 对象2: new String(“a”)
    • 对象3: 常量池中的"a"
    • 对象4: new String(“b”)
    • 对象5: 常量池中的"b"

深入剖析: StringBuilder的toString():

  • 对象6 :new String(“ab”)
  • 强调一下,toString()的调用,在字符串常量池中,没有生成"ab"

如图 没有在常量池放一个 ab 仅仅只是new了一个String

关于String.intern()的面试题

	    String s = new String("1");
        String s1 = s.intern();//调用此方法之前,字符串常量池中已经存在了"1"
        String s2 = "1";
        System.out.println(s == s2);//jdk6:false   jdk7/8:false

s 和s2放的内容不同

注:图中绿色线条代表 string 对象的内容指向。 黑色线条代表地址指向。

        //s3变量记录的地址为:new String("11")
		String s3 = new String("1") + new String("1"); //对象的地址 
        //执行完上一行代码以后,字符串常量池中,是否存在"11"呢?答案:不存在!!

        //在字符串常量池中生成"11"。如何理解:jdk6:创建了一个新的对象"11",也就有新的地址。
		// jdk7:此时常量中并没有创建"11",而是创建一个指向堆空间中new String("11")的地址	
		s3.intern(); //JDK 6常量池的地址
		

        //s4变量记录的地址:使用的是上一行代码代码执行时,在常量池中生成的"11"的地址
        String s4 = "11";
        System.out.println(s3 == s4);//jdk6:false  jdk7/8:true

​ jdk 6

JDK7

拓展

public class StringIntern1 {
    public static void main(String[] args) {
        //StringIntern.java中练习的拓展:
        String s3 = new String("1") + new String("1");//new String("11")
        //执行完上一行代码以后,字符串常量池中,是否存在"11"呢?答案:不存在!!
        String s4 = "11";//在字符串常量池中生成对象"11"
        String s5 = s3.intern();
        System.out.println(s3 == s4);//false
        System.out.println(s5 == s4);//true
    }
}

总结String的intern()的使用

  • jdk1.6中,将这个字符串对象尝试放入串池。
    • ➢如果字符串常量池中有,则并不会放入。返回已有的串池中的对象的地址
    • ➢如果没有,会把此对象复制一份,放入串池,并返回串池中的对象地址
  • Jdk1.7起,将这个字符串对象尝试放入串池。
    • ➢如果字符串常量池中有,则并不会放入。返回已有的串池中的对象的地址
    • ➢如果没有,则会把对象的引用地址复制一份,放入串池,并返回串池中的引用地址

练习

练习1
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
    }
}

jdk6

jdk7/8

练习2
public class StringExer2 {
    public static void main(String[] args) {
       //s1指向的是new的对象  S1是对象的地址  对象指向“ab”
        String s1 = new String("ab");//执行完以后,会在字符串常量池中会生成"ab" false
       
        //String s1 = new String("a") + new String("b");执行完以后,不会在字符串常量池中会生成"ab"  true
        s1.intern();
        String s2 = "ab";  // String s1 = new String("ab");情况下 s2直接指向""ab""
        //String s1 = new String("a") + new String("b");情况下 常量堆也指向s1对象
        Syst em.out.println(s1 == s2); 
    }
}

intern()效率测试

大的网站平台,需要内存中存储大量的字符串。比如社交网站,很多人都存储:北京市、海淀区等信息。这时候如果字符串都调用 intern()方法,就会明显降低内存的大小。

/**
 * 使用intern()测试执行效率:空间使用上
 *
 * 结论:对于程序中大量存在存在的字符串,尤其其中存在很多重复字符串时,使用intern()可以节省内存空间。
 *
 */
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();
    }
}
复制代码

6.StrtingTable的垃圾回收

/**
 * String的垃圾回收:
 * -Xms15m -Xmx15m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails
 *
 */
public class StringGCTest {
    public static void main(String[] args) {
//        for (int j = 0; j < 100; j++) {
//            String.valueOf(j).intern();
//        }
        //发生垃圾回收行为
        for (int j = 0; j < 100000; j++) {
            String.valueOf(j).intern();
        }
    }
}

img

7.G1中的String去重操作

  • 背景:对许多Java应用(有大的也有小的)做的测试得出以下结果:
    • ➢堆存活数据集合里面String对象占了25%
    • ➢堆存活数据集合里面重复的String对象有13.5%
    • ➢String对象的平均长度是45
  • 许多大规模的Java应用的瓶颈在于内存,测试表明,在这些类型的应用 里面,Java堆中存活的数据集合差不多258是String对象。更进一一步,这里面差不多一半String对象是重复的,重复的意思是说: string1. equals (string2)=true。堆上存在重复的string对象必然是一种内存的浪费。这个项目将在G1垃圾收集器中实现自动持续对重复的String对象进行去重,这样就能避免浪费内存。

实现

  • ➢当垃圾收集器工作的时候,会访问堆上存活的对象。对每一个访问的对象都会检查是否是候选的要去重的String对象。
  • ➢如果是,把这个对象的一个引用插入到队列中等待后续的处理。一个去重的线程在后台运行,处理这个队列。处理队列的一个元素意味着从队列删除这个元素,然后尝试去重它引用的String对象。
  • ➢使用一个hashtable来记录所有的被String对象使用的不重复的char数组。 当去重的时候,会查这个hashtable,来看堆上是否已经存在一个一模一样的char数组。
  • ➢如果存在,String对象会被调整引用那个数组,释放对原来的数组的引用,最终会被垃圾收集器回收掉。
  • ➢如果查找失败,char数组会被插入到hashtable,这样以后的时候就可以共享这个数组了。

命令行选项

  • ➢UseStringDeduplication (bool) :开启String去重,默认是不开启的,需要手动开启
  • ➢PrintStringDedupl icationStatistics (bool) :打印详细的去重统计信息,
  • ➢StringDedupl icationAgeThreshold (uintx) :达到这个年龄的string对象被认为是去重的候选对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值