2013-10-02 44 views
0

我尝试操纵大字符串流氓,并得到不断出现以下错误:java.lang.OutOfMemoryError:与流氓代码Java堆空间

java.lang.OutOfMemoryError: Java heap space(internal error).

我在eclipse.ini文件改变以下参数:

-XX:MaxPermSize=1024m 
-Xms256m 
-Xmx1024m 

但这并没有改变。

的代码看起来是这样的:

public str removeBB(str file){ 
while(contains(file, "aB")){ 
    index1 = findFirst(file, "aB"); 
    index2 = (findFirst(file, "Ba") + 2); 

    subString1 = substring(file, 0, index1); 
    subString2 = substring(file, index2); 

    file = subString1 + subString2; 
} 

return file; 
} 

我怎样才能防止这种错误?是否有方法来编写代码,以便更有效地利用内存?

+0

嘿,你没有改变'BB'的发生,这使得while循环无限循环。 – Raptor

回答

1

我没有立即看到为什么这段代码是抖动内存,也许是因为它在您的示例字符串中发现index2小于index1?

但无论如何,我会写像使用正则表达式和参观,而不是使用的indexOf:

visit (file) { case /aB.*Ba/ => "" }

此外,字符串的切片有一个更好的语法来使用,而不是子:

rascal>"asdlhfasldfhslf"[5..8] str: "fas"

+0

也许这样你也会松动内存垃圾的行为。你可以发表一个内存天高的例子吗? – jurgenv

相关问题