做了三个实验,之后我凌乱了
使用String
public class MemoryTest{ public static void main(String args[]){ System.out.print("String :"); String s="abcdefghijklmnopqrstuvwxyz"; System.out.print(" 当前虚拟机最大可用内存为 :"); System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M"); System.out.print(" 循环前,虚拟机已占用内存 :"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); int count = 0; while(true){ try{ s+=s; count++; System.out.println("String 实 际 字 节数 :"+s.length()+"b"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); } catch(Error o){ System.out.println(" 循环次数 :"+count); System.out.println("String 实 际 字 节数 :"+s.length()/1024/1024+"M"); System.out.print(" 循环后,已占用内存 :"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); System.out.println("Catch 到的错误 :"+o); break; } } } }
E:\javacode\java_programmer_work>javac MemoryTest.java E:\javacode\java_programmer_work>java MemoryTest String : 当前虚拟机最大可用内存为 :247M 循环前,虚拟机已占用内存 :15M String 实 际 字 节数 :52b 15M String 实 际 字 节数 :104b 15M String 实 际 字 节数 :208b 15M String 实 际 字 节数 :416b 15M String 实 际 字 节数 :832b 15M String 实 际 字 节数 :1664b 15M String 实 际 字 节数 :3328b 15M String 实 际 字 节数 :6656b 15M String 实 际 字 节数 :13312b 15M String 实 际 字 节数 :26624b 15M String 实 际 字 节数 :53248b 15M String 实 际 字 节数 :106496b 15M String 实 际 字 节数 :212992b 15M String 实 际 字 节数 :425984b 15M String 实 际 字 节数 :851968b 15M String 实 际 字 节数 :1703936b 15M String 实 际 字 节数 :3407872b 25M String 实 际 字 节数 :6815744b 50M String 实 际 字 节数 :13631488b 100M String 实 际 字 节数 :27262976b 201M 循环次数 :20 String 实 际 字 节数 :26M 循环后,已占用内存 :247M Catch 到的错误 :java.lang.OutOfMemoryError: Java heap space
使用StringBuilder(Java1.2之后推荐使用)
public class MemoryTest1{ public static void main(String args[]){ System.out.print("StringBuilder :"); StringBuilder s= new StringBuilder("abcdefghijklmnopqrstuvwxyz"); System.out.print("当前虚拟机最大可用内存为 :"); System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M"); System.out.print("循环前,虚拟机已占用内存 :"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); int count = 0; while(true){ try{ s.append(s); count++; System.out.println("String 实 际 字 节数 :"+s.length()+"b"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); } catch(Error o){ System.out.println("循环次数 :"+count); System.out.println("String 实 际 字 节数 :"+s.length()/1024/1024+"M"); System.out.print("循环后,已占用内存 :"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); System.out.println("Catch 到的错误 :"+o); break; } } } }
E:\javacode\java_programmer_work>javac MemoryTest1.java E:\javacode\java_programmer_work>java MemoryTest1 StringBuilder :当前虚拟机最大可用内存为 :247M 循环前,虚拟机已占用内存 :15M String 实 际 字 节数 :52b 15M String 实 际 字 节数 :104b 15M String 实 际 字 节数 :208b 15M String 实 际 字 节数 :416b 15M String 实 际 字 节数 :832b 15M String 实 际 字 节数 :1664b 15M String 实 际 字 节数 :3328b 15M String 实 际 字 节数 :6656b 15M String 实 际 字 节数 :13312b 15M String 实 际 字 节数 :26624b 15M String 实 际 字 节数 :53248b 15M String 实 际 字 节数 :106496b 15M String 实 际 字 节数 :212992b 15M String 实 际 字 节数 :425984b 15M String 实 际 字 节数 :851968b 15M String 实 际 字 节数 :1703936b 15M String 实 际 字 节数 :3407872b 26M String 实 际 字 节数 :6815744b 53M String 实 际 字 节数 :13631488b 107M String 实 际 字 节数 :27262976b 210M 循环次数 :20 String 实 际 字 节数 :26M 循环后,已占用内存 :247M Catch 到的错误 :java.lang.OutOfMemoryError: Java heap space
使用StringBuffer
public class MemoryTest2{ public static void main(String args[]){ System.out.print("StringBuffer :"); StringBuffer s= new StringBuffer("abcdefghijklmnopqrstuvwxyz"); System.out.print("当前虚拟机最大可用内存为 :"); System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M"); System.out.print("循环前,虚拟机已占用内存 :"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); int count = 0; while(true){ try{ s.append(s); count++; System.out.println("String 实 际 字 节数 :"+s.length()+"b"); System.out.println(+Runtime.getRuntime().totalMemory()/1024/1024+"Mb"); } catch(Error o){ System.out.println("循环次数 :"+count); System.out.println("String 实 际 字 节数 :"+s.length()/1024/1024+"M"); System.out.print("循环后,已占用内存 :"); System.out.println(Runtime.getRuntime().totalMemory()/1024/1024+"M"); System.out.println("Catch 到的错误 :"+o); break; } } } }
E:\javacode\java_programmer_work>javac MemoryTest2.java E:\javacode\java_programmer_work>java MemoryTest2 StringBuffer :当前虚拟机最大可用内存为 :247M 循环前,虚拟机已占用内存 :15M String 实 际 字 节数 :52b 15Mb String 实 际 字 节数 :104b 15Mb String 实 际 字 节数 :208b 15Mb String 实 际 字 节数 :416b 15Mb String 实 际 字 节数 :832b 15Mb String 实 际 字 节数 :1664b 15Mb String 实 际 字 节数 :3328b 15Mb String 实 际 字 节数 :6656b 15Mb String 实 际 字 节数 :13312b 15Mb String 实 际 字 节数 :26624b 15Mb String 实 际 字 节数 :53248b 15Mb String 实 际 字 节数 :106496b 15Mb String 实 际 字 节数 :212992b 15Mb String 实 际 字 节数 :425984b 15Mb String 实 际 字 节数 :851968b 15Mb String 实 际 字 节数 :1703936b 15Mb String 实 际 字 节数 :3407872b 26Mb String 实 际 字 节数 :6815744b 53Mb String 实 际 字 节数 :13631488b 107Mb String 实 际 字 节数 :27262976b 210Mb 循环次数 :20 String 实 际 字 节数 :26M 循环后,已占用内存 :247M Catch 到的错误 :java.lang.OutOfMemoryError: Java heap space
时间比较: 570,312,301
从实验可以看出 传说之中的StringBuidler StringBuffer 居然比String还费内存,tell me why???