Bug 49899 - PatternLayout and EnhancedPatternLayout issues with large messages
Summary: PatternLayout and EnhancedPatternLayout issues with large messages
Status: NEW
Alias: None
Product: Log4j - Now in Jira
Classification: Unclassified
Component: Layout (show other bugs)
Version: 1.2
Hardware: All All
: P2 normal
Target Milestone: 1.2.18
Assignee: log4j-dev
Depends on:
Reported: 2010-09-08 11:30 UTC by Stefan Reich
Modified: 2013-04-10 04:37 UTC (History)
0 users

Patch (2.09 KB, text/plain)
2010-09-08 11:30 UTC, Stefan Reich

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Reich 2010-09-08 11:30:32 UTC
Created attachment 26004 [details]

When logging large messages (SOAP messages in my case), the two Layout classes show up as a key contributor to memory pressure. Given a log entry size of say 1 megabyte, EnhancedPatternLayout.format will resize the StringBuffer from 16 to 33, 67, 135, ... creating a lot of unnecessary garbage along the way.
Even worse, PatternLayout holds on to that StringBuffer in a field, and doesn't allow the its contents to be gc'ed until the next invocation.

Proposed fix. 
* If the message argument is a String, use its length to preallocate the StringBuffer to reduce the chance of resizing.
* For PatternLayout, get rid of the StringBuffer field and allocate a new one each time, which is what EnhancedPatternLayout.format does, too

Patch attached