国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

邂逅StringIndexOutOfBoundsException

2019-11-18 15:17:43
字體:
來源:轉載
供稿:網友

  今天在WCS的測試中邂逅了這個從未接觸的exception

迫使我對它做了一些分析

首先:
“不斷的將被選中的字符串加到某一字符串末尾,當長度超過一定量時提示:
java.lang.StringIndexOutOfBoundsException: String index out of range: 10
”并不能說明String有長度限制

Java API指出StringIndexOutOfBoundsException異常
Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods sUCh as the charAt method。
上面的錯誤是因為
String.length()<10;
而你又要取index>=10的字符從而拋出上面異常
String其實是沒有限制的,而是當String太大了,超過JVM的自身的內存后會拋出
java.lang.OutOfMemoryError錯誤

String是沒有長度限制的,而是有JVM的內存限制了String的長度

在dayworker的blog中還提到

quote:


public class testString{
public static void main(String args[])
{
String s="abbbbb";
System.out.System.out.println("JVM IS USING MEMORY:"+Runtime.getRuntime().totalMemory()/1024/1024+"M");
Runtime.getRuntime().traceMethodCalls(true);
while(true)
{
try{
s=s+s;

}catch(Exception e)
{
System.out.println(e);
}
catch(Error o)
{ String unit = null;
int sizeb = s.length();
int size = sizeb;
int time = 0;
while(size>1024)
{
size = size/1024;
time++;
}
switch(time)
{
case 0: unit = "byte";break;
case 1: unit = "k"; break;
case 2: unit = "M"; break;
default : unit = "byte";
}

System.out.println("String has used memory:"+size+unit);
System.out.println("JVM IS USING MEMORY:"+(float)Runtime.getRuntime().totalMemory()/1024/1024+"M");
System.out.println("MemoryError:"+o);
break;
}

}
}
}
然后我們用JVM的默認參數執行(我的機器內存是128M)
java testString
結果:
JVM MAX MEMORY: 128M
JVM IS USING MEMORY:1M
String has used memory:12M
JVM IS USING MEMORY:63.5625M
MemoryError:java.lang.OutOfMemoryError
開始JVM使用的內存是1M,當String為12M,JVM使用了63M多時
JVM溢出。

然后,我們用限制JVM內存大小的參數來執行,限制最大內存5M
java -mx5m testString
結果:
JVM MAX MEMORY: 70M
JVM IS USING MEMORY:1M
String has used memory:768.0k
JVM IS USING MEMORY:5.9375M
MemoryError:java.lang.OutOfMemoryError
開始JVM使用的內存是1M,當String為768k,JVM使用了5M多時
JVM溢出。

大家還可以改變 -mx參數,來進一步做實驗。
以上兩個實驗證實,String是沒有長度限制的,而是有JVM的內存限制了String的長度。同時說明,并不會拋出任何Exception而只會拋出Error.

OutMemoryError表明程序的設計很差,或者碰到了超出編程人員所預想的大批量的數據。不管哪種情況,都只有下面這幾種解決辦法。它們是:

設計人員重新設計程序,不致使程序一次載入所有的數據。

數據可以分割成更小的塊。

可以為程序分配更多的內存。

為Java虛擬機提供更多的內存。

而上面的例子是為虛擬機提供更多的內存

=======================================
其實應該少用String這東西,非凡是 String的 +=操作 
不僅原來的String對象不能繼續使用,主要是又要new出N多的新對象出來,再多的memory也要out~~
String用char array實現,就肯定由長度限制的,不能用memory來衡量

==================================
例如上面的程序改用StringBuffer實現,就可以得到極大的改善。
下面是我改用StringBuffer做的測試:
注重:程序循環了2097150次!
是使用String的程序的99864倍!

public class TestStringBuffer{
public static void main(String args[])
{
String s="abbbbb";
StringBuffer sb = new StringBuffer(s);
System.out.println("JVM IS USING MEMORY:"+
(Runtime.getRuntime().totalMemory()/1024/1024)+
"M");
Runtime.getRuntime().traceMethodCalls(true);

int count = 0;
while(true)
{
try{
sb.append(s);
count++;

}catch(Exception e)
{
System.out.println(e);
}
catch(Error o)
{
String unit = null;
int size = sb.length();
size *= 2;

int time = 0;
while(size>1024)
{
size = size/1024;
time++;
}
switch(time)
{
case 0: unit = "byte";break;
case 1: unit = "k"; break;
case 2: unit = "M"; break;
default : unit = "byte";
}

System.out.println("Loop times:"+count);
System.out.println("String has used memory:"+size+unit);
System.out.println("JVM IS USING MEMORY:"+
(float)Runtime.getRuntime().totalMemory()/1024/1024+
"M");
System.out.println("MemoryError:"+o);
break;
}

}
}
}

輸出結果:
JVM IS USING MEMORY:1M
Loop times:2097150
String has used memory:23M
JVM IS USING MEMORY:63.75M
MemoryError:java.lang.OutOfMemoryError

=====================
從另一方面說,假如你要處理的字符串達到百兆甚至上GB,使用String對象,根本沒法工作,所以這個問題不需要太多討論。看一下jdk的源文件,String的長度是String對象的一個成員count,類型是int,不是long,也不是char。知道這些,我認為夠了。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海伦市| 梁河县| 青阳县| 贵溪市| 上高县| 奎屯市| 古田县| 石首市| 定州市| 社会| 古交市| 老河口市| 无棣县| 满洲里市| 剑川县| 河东区| 射阳县| 赣州市| 吉安市| 安徽省| 乌鲁木齐市| 乐平市| SHOW| 班戈县| 双城市| 教育| 永宁县| 政和县| 营山县| 翁牛特旗| 桃江县| 兴仁县| 博乐市| 马龙县| 澄江县| 杭州市| 枣阳市| 巍山| 新干县| 政和县| 镶黄旗|