Repository: logging-log4j2 Updated Branches: refs/heads/master db47cde3d -> 189c0d0b0
LOG4J2-1434 Ensure that the thread-local StringBuilders used to format log events to text will not retain excessive memory after a large message was logged. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/945cea3a Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/945cea3a Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/945cea3a Branch: refs/heads/master Commit: 945cea3a6ea405e2cddbd622e92f82590c6231d9 Parents: db47cde Author: rpopma <rpo...@apache.org> Authored: Sat Jun 18 16:05:34 2016 +0900 Committer: rpopma <rpo...@apache.org> Committed: Sat Jun 18 16:05:34 2016 +0900 ---------------------------------------------------------------------- .../logging/log4j/core/layout/AbstractStringLayout.java | 11 +++++++++++ src/changes/changes.xml | 4 ++++ src/site/xdoc/manual/configuration.xml.vm | 7 ++++++- 3 files changed, 21 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/945cea3a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java index 4d4e761..c2edd6f 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.util.Constants; import org.apache.logging.log4j.core.util.StringEncoder; +import org.apache.logging.log4j.util.PropertiesUtil; import org.apache.logging.log4j.util.Strings; import java.io.UnsupportedEncodingException; @@ -57,10 +58,16 @@ public abstract class AbstractStringLayout extends AbstractLayout<String> implem */ protected static final int DEFAULT_STRING_BUILDER_SIZE = 1024; + protected static final int MAX_STRING_BUILDER_SIZE = size("log4j.layoutStringBuilder.maxSize", 2 * 1024); + private static final ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<>(); private Encoder<StringBuilder> textEncoder; + private static int size(final String property, final int defaultValue) { + return PropertiesUtil.getProperties().getIntegerProperty(property, defaultValue); + } + /** * Returns a {@code StringBuilder} that this Layout implementation can use to write the formatted log event to. * @@ -72,6 +79,10 @@ public abstract class AbstractStringLayout extends AbstractLayout<String> implem result = new StringBuilder(DEFAULT_STRING_BUILDER_SIZE); threadLocal.set(result); } + if (result.length() > MAX_STRING_BUILDER_SIZE) { + result.setLength(MAX_STRING_BUILDER_SIZE); + result.trimToSize(); + } result.setLength(0); return result; } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/945cea3a/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0de777c..d5cf56a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,10 @@ </properties> <body> <release version="2.6.2" date="2016-MM-DD" description="GA Release 2.6.2"> + <action issue="LOG4J2-1434" dev="rpopma" type="fix" due-to="Luke Butters"> + Ensure that the thread-local StringBuilders used to format log events to text will not retain excessive + memory after a large message was logged. + </action> <action issue="LOG4J2-1395" dev="mikes" type="add"> Add "direct" option to ConsoleAppender for increased performance. </action> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/945cea3a/src/site/xdoc/manual/configuration.xml.vm ---------------------------------------------------------------------- diff --git a/src/site/xdoc/manual/configuration.xml.vm b/src/site/xdoc/manual/configuration.xml.vm index 1276a0e..65843e3 100644 --- a/src/site/xdoc/manual/configuration.xml.vm +++ b/src/site/xdoc/manual/configuration.xml.vm @@ -1985,7 +1985,12 @@ public class AwesomeTest { <td>In GC-free mode, this property determines the maximum size of the reusable StringBuilders where the message text is formatted and potentially passed to background threads.</td> </tr> - + <tr> + <td><a name="log4j.layoutStringBuilder.maxSize" />log4j.layoutStringBuilder.maxSize</td> + <td>2048</td> + <td>This property determines the maximum size of the thread-local reusable StringBuilders + used to format the log event to text by Layouts that extend AbstractStringLayout. + </tr> <tr> <td><a name="log4j.unbox.ringbuffer.size" />log4j.unbox.ringbuffer.size</td> <td>32</td>