2011年11月22日 星期二

[ Java 常見問題 ] 'java.lang.OutOfMemoryError: GC overhead limit exceeded'


參考至 這裡
問題描述 :
I get this error message as I execute my program :
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?

可能回答 :
1. GC 花太多時間在做 garbage collect (預設是 98%), 而太少時間在做你程式原本要做的事 :
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. Check out this article for details (specifically this part).

2. 你建立太多物件, 接著就 null 它們, 導致 GC 需要花很多力氣與時間去 garbage collect 它們
The GC throws this exception when too much time is spent in garbage collection for too little return, eg. 98% of CPU time is spent on GC and less than 2% of heap is recovered. 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.

You can turn this off with the command line option -XX:-UseGCOverheadLimit. More info here.
It's usually the code. Here's a simple example :
  1. import java.util.*;  
  2.   
  3. public class GarbageCollector {  
  4.   
  5.     public static void main(String... args) {  
  6.   
  7.         System.out.printf("Testing...%n");  
  8.         List list = new ArrayList();  
  9.         for (int outer = 0; outer < 10000; outer++) {  
  10.   
  11.             // list = new ArrayList(10000); // BAD  
  12.             // list = new ArrayList(); // WORSE  
  13.             list.clear(); // BETTER  
  14.   
  15.             for (int inner = 0; inner < 10000; inner++) {  
  16.                 list.add(Math.random());  
  17.             }  
  18.   
  19.             if (outer % 1000 == 0) {  
  20.                 System.out.printf("Outer loop at %d%n", outer);  
  21.             }  
  22.   
  23.         }  
  24.         System.out.printf("Done.%n");  
  25.     }  
  26. }  
Using java 1.6.0_24-b07 On a Windows7 32 bit. Key in command line below :
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...
This message was edited 6 times. Last update was at 23/11/2011 09:45:08

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...