package com.hhb.base;public class StringTest2 {
/** * @param args */ public static void main(String[] args) { long start=System.currentTimeMillis(); String hello="Hello"; for(int i=0;i<100;i++){ hello+=hello; } long end=System.currentTimeMillis(); System.out.println("String 累加消耗时间:"+(end-start)); start=System.currentTimeMillis(); StringBuffer buffer=new StringBuffer(); for(int i=0;i<10;i++){ buffer.append("hello").append(i); } end=System.currentTimeMillis(); System.out.println("String 累加消耗时间:"+(end-start)); start=System.currentTimeMillis(); StringBuilder builder=new StringBuilder(); for(int i=0;i<1000;i++){ builder.append("hello").append(i); } end=System.currentTimeMillis(); System.out.println("String 累加消耗时间:"+(end-start)); }}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:3209)
at java.lang.String.<init>(String.java:215)
at java.lang.StringBuilder.toString(StringBuilder.java:430)
at com.hhb.base.StringTest2.main(StringTest2.java:13)
碰到问题无解...
eclipse配置:
-startup
plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.0.200.v20090519
-product
org.eclipse.epp.package.jee.product
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms256m
-Xmx512m
-XX:+PrintGCTimeStamps
-XX:+PrintGCDetails
-verbose:gc
-Xloggc:gc.log
-XX:PermSize=96m
-XX:MaxPermSize=96m
-XX:+DisableExplicitGC
问题补充
dennis_zane 写道
跟性能没什么关系,问题是这段代码:
String hello="Hello";
for(int i=0;i<100;i++){
hello+=hello;
}
hello是以指数级增长的,不用99次方就撑爆内存了。
String hello="Hello";
for(int i=0;i<100;i++){
hello+=hello;
}
hello是以指数级增长的,不用99次方就撑爆内存了。
楼主一语点醒 谢谢...