Mike
Mike

Reputation: 689

log4j 1.2 - async logger memory usage limit setting

I'm wondering if there's a way to limit the amount of memory that the async logger will use. There is a buffer size that limits the number of logging elements before they get written to disk like this:

  <appender name="async" class="org.apache.log4j.AsyncAppender">
    <param name="BufferSize" value="1024"/>
    <appender-ref ref="ROLL"/>
  </appender>

however, there doesn't seem to be any way to limit the amount of memory that log4j will use before deciding to block and write out its queue. The default buffer size is 128 LoggingEvents, but each LoggingEvent could theoretically take up say 1 GB which could quickly take up all perm gen space.

A global setting over the whole queue would be best. We would rather not limit the amount of memory you have available for individual log calls, but that could be a good last resort.

Many thanks, Mike

Upvotes: 1

Views: 1310

Answers (1)

jihor
jihor

Reputation: 2599

No, in Log4j 1.2 the buffer is just an ArrayList. The sources are pretty straightforward. The only available option is blocking, which offers a choice to block the thread when the buffer is full. This will limit the memory consumption, but may also freeze the application, since the appender becomes synchronous at this point.

The buffer implementation was changed to a Queue in Log4j2, and a policy is available to set the appender behavior when the buffer is full.

Upvotes: 1

Related Questions