'java.lang.OutOfMemoryError: Java heap space with stackoverflow issue in log4j2
While calling logger.error(ex.getMessage(),ex) causing stackoverflow and java.lang.OutOfMemoryError: Java heap space with repeatative call of following stacktrace. it is happening randomly on our application. Log4j2 version is 2.17.0
at org.apache.logging.log4j.spi.AbstractLogger.handleLogMessageException(Ljava/lang/Throwable;Ljava/lang/String;Lorg/apache/logging/log4j/message/Message;)V (AbstractLogger.java:2225)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(Ljava/lang/String;Ljava/lang/StackTraceElement;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Lorg/apache/logging/log4j/message/Message;Ljava/lang/Throwable;)V (AbstractLogger.java:2208)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Lorg/apache/logging/log4j/message/Message;Ljava/lang/Throwable;)V (AbstractLogger.java:2159)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Lorg/apache/logging/log4j/message/Message;Ljava/lang/Throwable;)V (AbstractLogger.java:2142)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V (AbstractLogger.java:2058)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V (AbstractLogger.java:1931)
at org.apache.logging.log4j.spi.AbstractLogger.warn(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V (AbstractLogger.java:2805)
at org.apache.logging.log4j.spi.AbstractLogger.handleLogMessageException(Ljava/lang/Throwable;Ljava/lang/String;Lorg/apache/logging/log4j/message/Message;)V (AbstractLogger.java:2225)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(Ljava/lang/String;Ljava/lang/StackTraceElement;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Lorg/apache/logging/log4j/message/Message;Ljava/lang/Throwable;)V (AbstractLogger.java:2208)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Lorg/apache/logging/log4j/message/Message;Ljava/lang/Throwable;)V (AbstractLogger.java:2159)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Lorg/apache/logging/log4j/message/Message;Ljava/lang/Throwable;)V (AbstractLogger.java:2142)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Ljava/lang/String;Ljava/lang/Throwable;)V (AbstractLogger.java:2017)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Marker;Ljava/lang/String;Ljava/lang/Throwable;)V (AbstractLogger.java:1983)
at org.apache.logging.slf4j.Log4jLogger.error(Ljava/lang/String;Ljava/lang/Throwable;)V (Log4jLogger.java:319)
Lo4j2.xml configuration file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config"
packages="custom.appender">
<Appenders>
<Routing name="CustomRouting">
<Routes pattern="${sys:pathToLogfile}">
<Route>
<CustomRollingRandomAccessFile name="FILE"
fileName="${sys:pathToLogfile}/${date:yyyy-MM-dd}.log"
filePattern="${sys:pathToLogfile}/%d{yyyy-MM-dd}.log.gz"
immediateFlush="true" append="true">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss zzz} | [%t] %C{2} (%F:%L) - %m | %X%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${sys:pathToLogfile}">
<IfFileName glob="//*.log.gz" />
<IfLastModified age="60d" />
</Delete>
</DefaultRolloverStrategy>
</CustomRollingRandomAccessFile>
</Route>
</Routes>
</Routing>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss zzz} [%t] %C{2} (%F:%L) - %m | %X%n" />
</Console>
</Appenders>
<Loggers>
<Root level="ERROR" includeLocation="true" >
<AppenderRef ref="CustomRouting">
<ThreadContextMapFilter onMatch="DENY" onMismatch="ACCEPT">
<KeyValuePair key="Dir" value="customval" />
</ThreadContextMapFilter>
</AppenderRef>
</Root>
</Loggers>
</Configuration>
Solution 1:[1]
If you're running out of memory, it's totally random which operation will run into this condition. It could be logging, or it could be anything else.
You'll have to monitor your application and either
- provide more memory (if it shows that the allocated memory is not enough)
- or fix a memory leak. Proper monitoring will show you the memory use over time.
If memory use only ever goes up (and garbage collection doesn't bring it down to a constantly low level), chances are you have a memory leak. Allocating more memory in that case might buy you some time (e.g. you might be able to go two weeks), but it'll not fix the issue.
But there's most likely nothing wrong with logging. It just is the operation that happens to run when memory runs out due to some other faulty component, or low allocated resources.
Edit: Somehow I've missed the stackoverflow part of your question - likely the "once a week" misled me to preliminary conclusion.
Please edit your question with more information - e.g. how did you configure your logging, and if it always happens at the same time, is there some scheduled event (e.g. log rotation, or a scheduled-once-a-week-job) at that time?
If you're absolutely positive that this is a log4j-related problem, my assumption is that it's somewhere in the (unprovided) log4j-configuration, rather than a programming issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
