【发布时间】:2015-08-07 05:14:16
【问题描述】:
我正在摆弄 Java.lang.String 并在它们上使用“+”运算符。我很想知道为什么会得到以下输出:
使用下面的代码,我可以进行数千次迭代,并且不会抛出内存异常:
public static void main(String[] args) {
String str = "hello";
int count = 1;
while (true) {
System.out.println(count++);
str = str + "newString";
}
}
但是,当我将“str”添加到自身时,我会在 20-30 次迭代后得到 OutOfMemoryError 异常:
public static void main(String[] args) {
String str = "hello";
int count = 1;
while (true) {
System.out.println(count++);
str = str + "newString" +str;
}
}
我在 32 位操作系统上使用 eclipse,并且没有额外的参数,例如 Xms 或 Xmx
【问题讨论】:
-
正确的迭代次数??
-
sysout 字符串你会明白的
标签: java memory-management memory-leaks