2

我正在运行一个运行大约 1-2 小时的多线程导入。并在导入中,在将数据放入表中之前。我正在查

if(debug.isEnabled())
 logger.debug("Object="+MyObject);

whereMyObject使用方法ToStringBuilder中的toString

java.lang.OutOfMemoryError: GC overhead limit exceeded
        at java.util.Arrays.copyOfRange(Arrays.java:2694)
        at java.lang.String.<init>(String.java:203)
        at java.lang.StringBuffer.toString(StringBuffer.java:561)
        at org.apache.commons.lang3.builder.ToStringBuilder.toString(ToStringBuilder.java:1063)

我认为 toStringBuilder 导致了这个问题。我对么?如果是,有什么方法可以解决这个问题?

4

2 回答 2

5

Not necessarily. All that error means is that you're almost out of heap space, and the garbage collector is giving up on trying to reclaim space because it has run too much without reclaiming enough space. The fact that it happened at that point in the code doesn't necessarily mean anything. It could be that something entirely different ate up the space, but that call kicked off the GC one more time, when it finally gave up. You'd need to take a heap dump and look at it in a profiler like YourKit or VisualVM to see what's really going on.

于 2013-02-26T05:08:42.913 回答
0

您的对象在内存中构造了一个太大的字符串,可能在 toStringBuilder() 方法中。

为避免这种情况,请尝试按行导入数据库,例如

if (debug.isEnabled()) {

   // since you are importing rows it must be a collection/array
   logger.debug("Object=");
   for(Row r in MyObject.getRows()) {
      logger.debug(r);
   }
}

或者,在极少数情况下,如果您真的有一个大对象要记录,请通过流式传输内容来记录对象本身,而不是创建一个压倒性的字符串。

于 2013-02-26T05:37:58.030 回答