Monthly Archives: 十月 2013

java.lang.OutOfMemoryError: GC overhead limit exceeded

这篇文章写的

Description:
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 stop doing any progress but busying running only the garbage collection all the time.
This is not recoverable without a JVM restart.

Diagnosis:
1st.
Unfortunately, your code has “bad smell”. Somewhere of your code was creating tons of temporary objects and tons of weakly-referenced objects in an already very memory-constrained environment.

Example1:


import java.util.*;

public class GarbageCollector {

public static void main(String... args) {

System.out.printf("Testing...%n");
List list = new ArrayList();
for (int outer = 0; outer < 10000; outer++) {

// list = new ArrayList(10000); // BAD
// list = new ArrayList(); // 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");
}
}

2nd.

Try these options to help you analyzing.

-Xloggc:gc.log
-XX: +PrintGCDetails
Prints more details at garbage collection.
-XX: +PrintGCTimeStamps
Prints a time stamp representing the number of seconds since the HotSpot VM was launched until the garbage collection occurred.
-XX: +HeapDumpOnOutOfMemoryError
System will generate a heap dump file, which can be analyzed by Eclipse Memory Analyzer.
-XX: HeapDumpPath=/path/to/your/own/dumppath

Suggestion:
1. Try to start single JVM for each process, otherwise the programs would be hard to diagnose due to processes interference.
2. Try to reuse objects in your code, to reduce memory comsumption.
3. Increase the heap size by “-Xmx1G” when you’re sure that no memory leakout in your code. (Keep looking belows)

MISSLEADING resolution:
1. Increase the heap size by “-Xmx1G”.
This may reduce the occurrence of OOM, but your program would finally drain out all of your memory if you dont know why this happens.
As the famous says, “no matter how much you improve production, they would finally be drained out by endless comsumption” (Probably like this :))
2. -XX: -UseGCOverheadLimit to turn of the JVM “GC overhead limit exceeded” check.
This option is highly VM-specific and unstable. Not suggested.

Reference:
1. error-java-lang-outofmemoryerror-gc-overhead-limit-exceeded
2. diagnosing-javalangoutofmemory
3. gc tuning