当我执行JUnit测试时,我得到了这个错误消息:
java.lang.OutOfMemoryError: GC overhead limit exceeded
我知道什么是OutOfMemoryError,但是GC开销限制意味着什么?我怎么解决这个问题?
当我执行JUnit测试时,我得到了这个错误消息:
java.lang.OutOfMemoryError: GC overhead limit exceeded
我知道什么是OutOfMemoryError,但是GC开销限制意味着什么?我怎么解决这个问题?
当前回答
通过设置这个选项,稍微增加堆的大小
运行→运行配置→参数→虚拟机参数
-Xms1024M -Xmx2048M
Xms—用于最小限制
Xmx -表示最大限制
其他回答
通常是代码。这里有一个简单的例子:
import java.util.*;
public class GarbageCollector {
public static void main(String... args) {
System.out.printf("Testing...%n");
List<Double> list = new ArrayList<Double>();
for (int outer = 0; outer < 10000; outer++) {
// list = new ArrayList<Double>(10000); // BAD
// list = new ArrayList<Double>(); // WORSE
list.clear(); // BETTER
for (int inner = 0; inner < 10000; inner++) {
list.add(Math.random());
}
if (outer % 1000 == 0) {
System.out.printf("Outer loop at %d%n", outer);
}
}
System.out.printf("Done.%n");
}
}
在Windows 7 32位操作系统上使用Java 1.6.0_24-b07。
java -Xloggc:gc.log GarbageCollector
然后查看gc.log
使用BAD方法触发444次 使用WORSE方法触发666次 使用BETTER方法触发354次
现在承认,这不是最好的测试或最好的设计,但当你面临别无选择只能实现这样的循环或处理行为糟糕的现有代码时,选择重用对象而不是创建新对象可以减少垃圾收集器阻碍的次数……
要在IntelliJ IDEA中增加堆大小,请遵循以下说明。这对我很管用。
对于Windows用户,
转到安装IDE的位置并搜索以下内容。
idea64.exe.vmoptions
编辑该文件并添加以下内容。
-Xms512m
-Xmx2024m
-XX:MaxPermSize=700m
-XX:ReservedCodeCacheSize=480m
就是这样!!
引用Oracle的文章“Java SE 6 HotSpot[tm]虚拟机垃圾收集调优”:
Excessive GC Time and OutOfMemoryError The parallel collector will throw an OutOfMemoryError if too much time is being spent in garbage collection: if more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, an OutOfMemoryError will be thrown. This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small. If necessary, this feature can be disabled by adding the option -XX:-UseGCOverheadLimit to the command line.
编辑:看起来有人打字比我快:)
在Netbeans中,设计最大堆大小可能会有所帮助。执行命令Run =>设置项目配置=>自定义。在弹出窗口的运行中,进入虚拟机选项,填写-Xms2048m -Xmx2048m。它可以解决堆大小的问题。
Java堆大小描述(xms, xmx, xmn)
-Xms size in bytes
Example : java -Xms32m
设置Java堆的初始大小。 默认大小为2097152 (2MB)。 该值必须是1024字节(1KB)的倍数且大于1024字节。 (-server标志将默认大小增加到32M。)
-Xmn size in bytes
Example : java -Xmx2m
设置Eden生成的初始Java堆大小。 默认值为640K。 (-server标志将默认大小增加到2M。)
-Xmx size in bytes
Example : java -Xmx2048m
设置Java堆可以增长到的最大大小。 默认大小为64M。 (-server标志将默认大小增加到128M。) 最大堆限制大约是2 GB (2048MB)。
Java内存参数(xms, xmx, xmn)格式化
在设置Java堆大小时,应该使用字母“m”或“m”表示MB,或使用字母“g”或“g”表示GB来指定内存参数。如果指定“MB”或“GB”,则该设置将不起作用。有效参数是这样的:
-Xms64m或-Xms64m -Xmx1g或-Xmx1g 还可以用2048MB指定2GB吗 另外,确保在指定参数时使用整数。使用-Xmx512m是一个有效的选项,但是-Xmx0.5g会导致错误。
这种推荐对某些人是有帮助的。