简体   繁体   中英

toStringBuilder causing issues

I am running a multithreaded import which runs for around 1-2 hours. and in the import, before putting data into the table. i am checking

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

where MyObject uses the ToStringBuilder in the toString method.

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)

I am thinking that toStringBuilder is causing this issues. am i correct? if yes what are ways to fix this?

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.

Your object construct a too-large string in memory, probably within the toStringBuilder() method.

To avoid this, try to do importing as per row into your database like

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);
   }
}

or, in rare case, should you really have a big object to log, have the object log itself by streaming content to log, rather than creating an overwhelming string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM