tkowalcz / tjahzi

Java clients, log4j2 and logback appenders for Grafana Loki
MIT License
106 stars 18 forks source link

Appender OutOf Memory Error for MuleSoft Apps #121

Closed KHALIFA-SH closed 11 months ago

KHALIFA-SH commented 11 months ago

Discussed in https://github.com/tkowalcz/tjahzi/discussions/120

Originally posted by **KHALIFA-SH** December 16, 2023 I have Used the log4j2 appender in MuleSoft project and I'm getting an outOfMemory Error when sending logs to Loki. Attached the stack trace... When I upgrade the worker size to at least 0.2 V core I don't see this error. I just want to ask if there is something I can do to avoid this error with minimal worker size (.01 v core). Thank you! Error Stack ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Initializing app 'grafana-loki-test' + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ERROR StatusConsoleListener Could not create plugin of type class pl.tkowalcz.tjahzi.log4j2.LokiAppender for element Loki: java.lang.OutOfMemoryError: Direct buffer memory java.lang.OutOfMemoryError: Direct buffer memory at java.nio.Bits.reserveMemory(Bits.java:695) at java.nio.DirectByteBuffer.(DirectByteBuffer.java:123) at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311) at pl.tkowalcz.tjahzi.TjahziInitializer.allocateJavaBuffer(TjahziInitializer.java:101) at pl.tkowalcz.tjahzi.TjahziInitializer.createLoggingSystem(TjahziInitializer.java:32) at pl.tkowalcz.tjahzi.log4j2.LokiAppenderBuilder.build(LokiAppenderBuilder.java:152) at pl.tkowalcz.tjahzi.log4j2.LokiAppenderBuilder.build(LokiAppenderBuilder.java:30) .... ERROR StatusConsoleListener Unable to invoke factory method in class pl.tkowalcz.tjahzi.log4j2.LokiAppender for element Loki: java.lang.IllegalStateException: No factory method found for class pl.tkowalcz.tjahzi.log4j2.LokiAppender java.lang.IllegalStateException: No factory method found for class pl.tkowalcz.tjahzi.log4j2.LokiAppender at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:260) at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:136) at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063) ....
kshkur commented 11 months ago

Fixed with "bufferSizeMegabytes" allocation

tkowalcz commented 11 months ago

Hi. Sorry for a late answer. The JVM has a user-settable upper limit on the maximum amount of allocatable direct buffer memory. It can be controlled by -XX:MaxDirectMemorySize=<size> option to java invocation. The stack trace indicates that you hit that limit.

The solution is to set smaller buffer like @kshkur suggests or set useOffHeapBuffer to false in Tjahzi configuration so that the buffer will be allocated on java heap.