专栏名称: 武哥聊编程
这里有技术,有段子,有生活,也有资源,要不然怎么叫 “私房菜” 呢?
目录
相关文章推荐
程序员小灰  ·  疯了!下载 DeepSeek 最高判20年! ·  3 天前  
银行家杂志  ·  金融监管总局最新公布! ·  昨天  
银行家杂志  ·  养老金融三支柱体系建设的挑战与机遇 ·  2 天前  
OSC开源社区  ·  DeepSeek-V3满血版在国产沐曦GPU ... ·  2 天前  
中国人民银行  ·  李强主持召开国务院第七次全体会议 ... ·  2 天前  
51好读  ›  专栏  ›  武哥聊编程

String字符串性能优化的几种方案

武哥聊编程  · 公众号  ·  · 2020-12-01 08:50

正文

作者: 朱季谦

来源: https://www.cnblogs.com/zhujiqian/p/12202951.html

String字符串是系统里最常用的类型之一,在系统中占据了很大的内存,因此,高效地使用字符串,对系统的性能有较好的提升。

针对字符串的优化,我在工作与学习过程总结了以下三种方案作分享:

一.优化构建的超大字符串

验证环境: jdk1.8

反编译工具: jad

1.下载反编译工具jad,百度云盘下载:

链接: https://pan.baidu.com/s/1TK1_N769NqtDtLn28jR-Xg

提取码: ilil

2.验证

先执行一段例子1代码:

public class test3 {public static void main(String[] args) {        String str="ab"+"cd"+"ef"+"123";    }}

执行完成后,用反编译工具jad进行反编译: jad -o -a -s d.java test.class

反编译后的代码:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.// Jad home page: http://www.kpdus.com/jad.html// Decompiler options: packimports(3) annotate // Source File Name:   test.javapackage example;public class test{public test(){//    0    0:aload_0         //    1    1:invokespecial   #1   //    2    4:return              }public static void main(String args[]){        String str = "abcdef123";//    0    0:ldc1            #2   //    1    2:astore_1        //    2    3:return              }}

案例2:

public class test1 {public static void main(String[] args)    {        String s = "abc";        String ss = "ok" + s + "xyz" + 5;        System.out.println(ss);    }}

用反编译工具jad执行jad -o -a -s d.java test1.class进行反编译后:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.




    
// Jad home page: http://www.kpdus.com/jad.html// Decompiler options: packimports(3) annotate // Source File Name:   test1.java
package example;
import java.io.PrintStream;
public class test1{public test1(){// 0 0:aload_0 // 1 1:invokespecial #1 // 2 4:return }public static void main(String args[]){ String s = "abc";// 0 0:ldc1 #2 // 1 2:astore_1 String ss = (new StringBuilder()).append("ok").append(s).append("xyz").append(5).toString();// 2 3:new #3 // 3 6:dup // 4 7:invokespecial #4 // 5 10:ldc1 #5 // 6 12:invokevirtual #6 // 7 15:aload_1 // 8 16:invokevirtual #6 // 9 19:ldc1 #7 // 10 21:invokevirtual #6 // 11 24:iconst_5 // 12 25:invokevirtual #8 // 13 28:invokevirtual #9 // 14 31:astore_2 System.out.println(ss);// 15 32:getstatic #10 // 16 35:aload_2 // 17 36:invokevirtual #11 // 18 39:return }}

根据反编译结果,可以看到内部其实是通过StringBuilder进行字符串拼接的。

再来执行例3的代码:

public class test2 {public static void main(String[] args) {        String s = "";        Random rand = new Random();for (int i = 0; i < 10; i++) {            s = s + rand.nextInt(1000) + " ";        }        System.out.println(s);    }}

用反编译工具jad执行jad -o -a -s d.java test2.class进行反编译后,发现其内部同样是通过StringBuilder来进行拼接的:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.// Jad home page: http://www.kpdus.com/jad.html// Decompiler options: packimports(3) annotate // Source File Name:   test2.javapackage example;import java.io.PrintStream;import java.util.Random;public class test2{public test2(){//    0    0:aload_0         //    1    1:invokespecial   #1   //    2    4:return              }public static void main(String args[]){        String s = "";//    0    0:ldc1            #2   




    
//    1    2:astore_1                Random rand = new Random();//    2    3:new             #3   //    3    6:dup             //    4    7:invokespecial   #4   //    5   10:astore_2        for(int i = 0; i < 10; i++)//*   6   11:iconst_0        //*   7   12:istore_3        //*   8   13:iload_3         //*   9   14:bipush          10//*  10   16:icmpge          55            s = (new StringBuilder()).append(s).append(rand.nextInt(1000)).append(" ").toString();//   11   19:new             #5   //   12   22:dup             //   13   23:invokespecial   #6   //   14   26:aload_1         //   15   27:invokevirtual   #7   //   16   30:aload_2         //   17   31:sipush          1000//   18   34:invokevirtual   #8   //   19   37:invokevirtual   #9   //   20   40:ldc1            #10  //   21   42:invokevirtual   #7   //   22   45:invokevirtual   #11  //   23   48:astore_1
// 24 49:iinc 3 1//* 25 52:goto 13 System.out.println(s);// 26 55:getstatic #12 // 27 58:aload_1 // 28 59:invokevirtual #13 // 29 62:return }}

综上案例分析,发现字符串进行“+”拼接时,内部有以下几种情况:

1.“+”直接拼接的是常量变量,如"ab"+"cd"+"ef"+"123",内部编译就把几个连接成一个常量字符串处理;

2. “+”拼接的含变量字符串,如案例2: "ok" + s + "xyz" + 5,内部编译其实是new 一个StringBuilder来进行来通过append进行拼接;

3.案例3循环过程,实质也是“+”拼接含变量字符串,因此,内部编译时,也会创建StringBuilder来进行拼接。

对比三种情况,发现第三种情况每次做循环,都会新创建一个StringBuilder对象,这会增加系统的内存,反过来就会降低系统性能。

因此,在做字符串拼接时,单线程环境下,可以显性使用StringBuilder来进行拼接,避免每循环一次就new一个StringBuilder对象; 在多线程环境下,可以使用线程安全的StringBuffer,但涉及到锁竞争,StringBuffer性能会比StringBuilder差一点。

这样,起到在字符串拼接时的优化效果。

2.如何使用String.intern节省内存?

在回答这个问题之前,可以先对一段代码进行测试:

1.首先在idea设置-XX:+PrintGCDetails -Xmx6G -Xmn3G,用来打印GC日志信息,设置如下图所示:

2.执行以下例子代码:

public class test4 {public static void main(String[] args) {        final int MAX=10000000;        System.out.println("不用intern:"+notIntern(MAX));//      System.out.println("使用intern:"+intern(MAX));    }private static long notIntern(int MAX){long start = System.currentTimeMillis();for (int i = 0; i < MAX; i++) {int j = i % 100;            String str = String.valueOf(j);        }return System.currentTimeMillis() - start;    }




    
/*    private static long intern(int MAX){        long start = System.currentTimeMillis();        for (int i = 0; i < MAX; i++) {            int j = i % 100;            String str = String.valueOf(j).intern();        }        return System.currentTimeMillis() - start;    }*/

未使用intern的GC日志:

不用intern:354[GC (System.gc()) [PSYoungGen: 377487K->760K(2752512K)] 377487K->768K(2758656K), 0.0009102 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (System.gc()) [PSYoungGen: 760K->0K(2752512K)] [ParOldGen: 8K->636K(6144K)] 768K->636K(2758656K), [Metaspace: 3278K->3278K(1056768K)], 0.0051214 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] HeapPSYoungGen      total 2752512K, used 23593K [0x0000000700000000, 0x00000007c0000000, 0x00000007c0000000)eden space 2359296K, 1% used [0x0000000700000000,0x000000070170a548,0x0000000790000000)from space 393216K, 0% used [0x0000000790000000,0x0000000790000000,0x00000007a8000000)to   space 393216K, 0% used [0x00000007a8000000,0x00000007a8000000,0x00000007c0000000)ParOldGen       total 6144K, used 636K [0x0000000640000000, 0x0000000640600000, 0x0000000700000000)object space 6144K, 10% used [0x0000000640000000,0x000000064009f2f8,0x0000000640600000)Metaspace       used 3284K, capacity 4500K, committed 4864K, reserved 1056768Kclass space    used 359K, capacity 388K, committed 512K, reserved 1048576K

根据打印的日志分析: 没有使用intern情况下, 执行时间为354ms,占用内存为24229k ;

使用intern的GC日志:

使用intern:1515[GC (System.gc()) [PSYoungGen: 613417K->1144K(2752512K)] 613417K->1152K(2758656K), 0.0012530 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (System.gc()) [PSYoungGen: 1144K->0K(2752512K)] [ParOldGen: 8K->965K(6144K)] 1152K->965K(2758656K), [Metaspace: 3780K->3780K(1056768K)], 0.0079962 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] HeapPSYoungGen      total 2752512K, used 15729K [0x0000000700000000, 0x00000007c0000000, 0x00000007c0000000)eden space 2359296K, 0% used [0x0000000700000000,0x0000000700f5c400,0x0000000790000000)from space 393216K, 0% used [0x0000000790000000,0x0000000790000000,0x00000007a8000000)to   space 393216K, 0% used [0x00000007a8000000,0x00000007a8000000,0x00000007c0000000)ParOldGen       total 6144K, used 965K [0x0000000640000000, 0x0000000640600000, 0x0000000700000000)object space 6144K, 15% used [0x0000000640000000,0x00000006400f1740,0x0000000640600000)Metaspace       used 3786K, capacity 4540K, committed 4864K, reserved 1056768Kclass space    used 420K, capacity 428K, committed 512K, reserved 1048576K

日志分析: 没有使用intern情况下, 执行时间为1515ms,占用内存为16694k;

综上所述:使用intern情况下,内存相对没有使用intern的情况要小,但在节省内存的同时,增加了时间复杂度。 我试过将MAX=10000000再增加一个0的情况下,使用intern将会花费高达11秒的执行时间,可见,在遍历数据过大时,不建议使用intern。

因此,使用intern的前提,一定要考虑到具体的使用场景。

到这里,可以确定, 使用String.intern确实可以节省内存。

接下来,分析一下intern在不同JDK版本的区别。

在JDK1.6中,字符串常量池在方法区中,方法区属于永久代。

在JDK1.7中,字符串常量池移到了堆中。

在JDK1.8中,字符串常量池移到了元空间里,与堆相独立。

分别在1.6、1.7、1.8版本执行以下一个例子:

public class test5 {public static void main(String[] args) {
String s1=new String("ab"); s.intern();String s2="ab"; System.out.println(s1==s2);

String s3=new String("ab")+new String("cd"); s3.intern();String s4="abcd"; System.out.println(s4==s3); }}

1.6版本

执行结果:

fasle false

分析:

执行第一部分时:







请到「今天看啥」查看全文