String字符串是系統裡最常用的類型之一,在系統中佔據了很大的內存,因此,高效地使用字符串,對系統的性能有較好的提升。
針對字符串的優化,我在工作與學習過程總結了以下三種方案作分享:
一.優化構建的超大字符串
驗證環境:jdk1.8
反編譯工具:jad
2.驗證
先執行一段例子1代碼:
<code>public class test3 { public static void main(String[] args) { String str="ab"+"cd"+"ef"+"123"; }}/<code>
執行完成後,用反編譯工具jad進行反編譯:jad -o -a -s d.java test.class
反編譯後的代碼:
<code>// 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 <method> // 2 4:return } public static void main(String args[]) { String str = "abcdef123"; // 0 0:ldc1 #2 <string> // 1 2:astore_1 // 2 3:return }}/<string>/<method>/<code>
案例2:
<code>public class test1 { public static void main(String[] args) { String s = "abc"; String ss = "ok" + s + "xyz" + 5; System.out.println(ss); }}/<code>
用反編譯工具jad執行jad -o -a -s d.java test1.class進行反編譯後:
<code>// 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.javapackage example;import java.io.PrintStream;public class test1{ public test1() { // 0 0:aload_0 // 1 1:invokespecial #1 <method> // 2 4:return } public static void main(String args[]) { String s = "abc"; // 0 0:ldc1 #2 <string> // 1 2:astore_1 String ss = (new StringBuilder()).append("ok").append(s).append("xyz").append(5).toString(); // 2 3:new #3 <class> // 3 6:dup // 4 7:invokespecial #4 <method> // 5 10:ldc1 #5 <string> // 6 12:invokevirtual #6 <method> // 7 15:aload_1 // 8 16:invokevirtual #6 <method> // 9 19:ldc1 #7 <string> // 10 21:invokevirtual #6 <method> // 11 24:iconst_5 // 12 25:invokevirtual #8 <method> // 13 28:invokevirtual #9 <method> // 14 31:astore_2 System.out.println(ss); // 15 32:getstatic #10 <field> // 16 35:aload_2 // 17 36:invokevirtual #11 <method> // 18 39:return }}/<method>/<field>/<method>/<method>/<method>/<string>/<method>/<method>/<string>/<method>/<class>/<string>/<method>/<code>
根據反編譯結果,可以看到內部其實是通過StringBuilder進行字符串拼接的。
再來執行例3的代碼:
<code>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); }}/<code>
用反編譯工具jad執行jad -o -a -s d.java test2.class進行反編譯後,發現其內部同樣是通過StringBuilder來進行拼接的:
<code>// 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 <method> // 2 4:return } public static void main(String args[]) { String s = ""; // 0 0:ldc1 #2 <string> // 1 2:astore_1 Random rand = new Random(); // 2 3:new #3 <class> // 3 6:dup // 4 7:invokespecial #4 <method> // 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 <class> // 12 22:dup // 13 23:invokespecial #6 <method> // 14 26:aload_1 // 15 27:invokevirtual #7 <method> // 16 30:aload_2 // 17 31:sipush 1000 // 18 34:invokevirtual #8 <method> // 19 37:invokevirtual #9 <method> // 20 40:ldc1 #10 <string> // 21 42:invokevirtual #7 <method> // 22 45:invokevirtual #11 <method> // 23 48:astore_1 // 24 49:iinc 3 1 //* 25 52:goto 13 System.out.println(s); // 26 55:getstatic #12 <field> // 27 58:aload_1 // 28 59:invokevirtual #13 <method> // 29 62:return }}/<method>/<field>/<method>/<method>/<string>/<method>/<method>/<method>/<method>/<class>/<method>/<class>/<string>/<method>/<code>
綜上案例分析,發現字符串進行“+”拼接時,內部有以下幾種情況:
1.“+”直接拼接的是常量變量,如"ab"+"cd"+"ef"+"123",內部編譯就把幾個連接成一個常量字符串處理;
2. “+”拼接的含變量字符串,如案例2:"ok" + s + "xyz" + 5,內部編譯其實是new 一個StringBuilder來進行來通過append進行拼接;
3.案例3循環過程,實質也是“+”拼接含變量字符串,因此,內部編譯時,也會創建StringBuilder來進行拼接。
對比三種情況,發現第三種情況每次做循環,都會新創建一個StringBuilder對象,這會增加系統的內存,反過來就會降低系統性能。
因此,在做字符串拼接時,單線程環境下,可以顯性使用StringBuilder來進行拼接,避免每循環一次就new一個StringBuilder對象;在多線程環境下,可以使用線程安全的StringBuffer,但涉及到鎖競爭,StringBuffer性能會比StringBuilder差一點。
這樣,起到在字符串拼接時的優化效果。
二.如何使用String.intern節省內存?
在回答這個問題之前,可以先對一段代碼進行測試:
1.首先在idea設置-XX:+PrintGCDetails -Xmx6G -Xmn3G,用來打印GC日誌信息,設置如下圖所示:
2.執行以下例子代碼:
<code>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; }*//<code>
未使用intern的GC日誌:
<code>不用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] Heap PSYoungGen 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 1056768K class space used 359K, capacity 388K, committed 512K, reserved 1048576K/<code>
根據打印的日誌分析:沒有使用intern情況下,執行時間為354ms,佔用內存為24229k;
使用intern的GC日誌:
<code>使用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] Heap PSYoungGen 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 1056768K class space used 420K, capacity 428K, committed 512K, reserved 1048576K/<code>
日誌分析:沒有使用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版本執行以下一個例子:
<code>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); }}/<code>
1.6版本
執行結果:
fasle false
分析:
執行第一部分時:
1.代碼編譯時,先在字符串常量池裡創建常量“ab";在調用new時,將在堆中創建一個String對象,字符串常量創建的“ab"存儲到堆中,最後堆中的String對象返回一個引用給s1。
2.s.intern(),在字符串常量池裡已經存在“ab”,便不再創建存放副本“ab";
3.s2="ab",s2指向的是字符串常量池裡”ab",而s1指向的堆中的”ab",故兩者不相等。
該示意圖如下:
執行第二部分:
1.兩個new出來相加的“abcd”存放在堆中,s3指向堆中的“abcd";
2.執行s3.intern(),在將“abcd"副本的存放到字符串常量池時,發現常量池裡沒有該”abcd",因此,成功存放;
3.s4="abcd"指向的是字符串常量池裡已有的“abcd"副本,而s3指向的是堆中的"abcd",副本"abcd"的地址和堆中“abcd"地址不相同,故為false;
1.7版本
false true
執行第一部分:這一部分與jdk1.6基本類似,不同在於,s1.intern()返回的是引用,而不是副本。
執行第二部分:
1.new String("ab")+new String("cd"),先在常量池裡生成“ab"和”cd",再在堆中生成“abcd";
2.執行s3.intern()時,會把“abcd”的對象引用放到字符串常量池裡,發現常量池裡還沒有該引用,故可成功放入。當String s4="abcd",即把字符串常量池中”abcd“的引用地址賦值給s4,相當於s4指向了堆中”abcd"的地址,故s3==s4為true。
1.8版本
false true
參考網上一些博客,在1.8版本當中,使用intern()時,執行原理如下:
若字符串常量池中,包含了與當前對象相當的字符串,將返回常量池裡的字符串;若不存在,則將該字符串存放進常量池裡,並返回字符串的引用。
綜上所述,可見三種版本當中,使用intern時,若字符串常量池裡不存在相應字符串時,存在以下區別:
例如:
String s1=new String("ab"); s.intern();
jdk1.6:若字符串常量池裡沒有“ab",則會在常量池裡存放一個“ab"副本,該副本地址與堆中的”ab"地址不相等;
jdk1.7:若字符串常量池裡沒有“ab",會將“ab”的對象引用放到字符串常量池裡,該引用地址與堆中”ab"的地址相同;
jdk1.8:若字符串常量池中包含與當前對象相當的字符串,將返回常量池裡的字符串;若不存在,則將該字符串存放進常量池裡,並返回字符串的引用。
三.如何使用字符串的分割方法?
在簡單進行字符串分割時,可以用indexOf替代split,因為split的性能不夠穩定,故針對簡單的字符串分割,可優先使用indexOf代替;
閱讀更多 老男孩的成長之路 的文章