Sreeja Mohan Sreeja Mohan - 7 months ago 27
Java Question

Can logging a huge message (around 90 MB) cause OutOfMemory Error? (Max heap size : 2048M)

Actually, I receive a message through RabbitMQ which is large.
While consuming this message, we log it."payload: {}", payload);

I see the following exception in logs at the logger statement line.

Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(
at java.lang.AbstractStringBuilder.expandCapacity(
at java.lang.AbstractStringBuilder.ensureCapacityInternal(
at java.lang.AbstractStringBuilder.append(
at java.lang.StringBuffer.append(
at org.apache.log4j.helpers.PatternParser$LiteralPatternConverter.format(
at org.apache.log4j.PatternLayout.format(
at org.graylog2.GelfMessageFactory.makeMessage(
at org.graylog2.log.GelfAppender.append(
at org.apache.log4j.AppenderSkeleton.doAppend(
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(
at org.apache.log4j.Category.callAppenders(
at org.apache.log4j.Category.forcedLog(
at org.apache.log4j.Category.log(

After getting rid of the logger statement, the message was processed successfully.

So, can I be sure that the logger was causing the
Or it is something else that is causing the

Heap size is : 2048M


Q: So, can I be sure that the logger was causing the OutOfMemory error? Or it is something else that is causing the OutOfMemory Error?

A: Based on the stack trace, yes.

From the trace you can evidently see that your log4j module (which is the logger object) was trying to perform a copyOf() operation but before the copy was done the program died with OutOfMemoryException.

As to how you would generally read the stack trace, this stackoverflow post can be a great help.

How to read and understand the java stack trace?


Generally the exact reason for the Exception is at the first line of your Stack Trace, and for more information about the cause of that exception, you need to gradually move down, and the root cause can often be found somewhere near the bottom of the stack trace.

I generally read the first line of the stack trace to determine what is happening, for your cause would be the OOM error, then I move to the last line and read from bottom up to see which object or area of code that could have caused the fault.