下面把相关的内从从stackoverflow拷贝过来
I get this error message as I execute my JUnit tests:
java.lang.OutOfMemoryError: GC overhead limit exceeded
I know what an OutOfMemoryError
is, but what does GC overhead limit mean? How can I solve this?
4 Answers
This message means that for some reason the garbage collector is taking an excessive amount of time (by default 98% of all CPU time of the process) and recovers very little memory in each run (by default 2% of the heap).
This effectively means that your program stops doing any progress and is busy running only the garbage collection at all time.
To prevent your application from soaking up CPU time without getting anything done, the JVM throws this Error
so that you have a chance of diagnosing the problem.
The rare cases where I've seen this happen is where some code was creating tons of temporary objects and tons of weakly-referenced objects in an already very memory-constrained environment.
|
Would it be correct to summarise your answer as follows: "It's just like an 'Out of Java Heap space' error. Give it more memory with -Xmx." ? –
Tim Cooper
Jun 19 '10 at 13:28
| ||
|
@Tim: No, that wouldn't be correct. While giving it more memory could reduce the problem, you should also look at your code and see why it produces that amount of garbage and why your code skims just below the "out of memory" mark. It's often a sign of broken code. –
Joachim Sauer
Jun 20 '10 at 15:48
| ||
|
Thanks, it seems Oracle isn't actually that good in data migration, they broke the link. –
Joachim Sauer
Nov 29 '10 at 19:30
| ||
| |||
|
@Guus: if multiple applications run in the same JVM, then yes, they can easily influence each other. It'll be hard to tell which one is misbehaving. Separating the applications into distinct JVMs might be the easiest solution. –
Joachim Sauer
Mar 1 at 11:48 |
下面是一个具体的例子
It's usually the code. Here's a simple example:
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");
}
}
Using java 1.6.0_24-b07 On a Windows7 32 bit.
java -Xloggc:gc.log GarbageCollector
Then look at gc.log
- Triggered 444 times using BAD method
- Triggered 666 times using WORSE method
- Triggered 354 times using BETTER method
Now granted, this is not the best test or the best design but when faced with a situation where you have no choice but implementing such a loop or when dealing with existing code that behaves badly, choosing to reuse objects instead of creating new ones can reduce the number of times the garbage collector gets in the way...