[ 
https://issues.apache.org/jira/browse/LOG4J2-1683?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15648076#comment-15648076
 ] 

Remko Popma commented on LOG4J2-1683:
-------------------------------------

Don't worry, I would be careful to discuss that first.

I'm leaving the current method as it is:
{code}
    public Map<String, String> getData() {
        return Collections.unmodifiableMap(data);
    }
{code}

And I added this method:
{code}
    public String getDataValue(String key) {
        return data.get(key);
    }
{code}

Client code can now call the {{getDataValue(String)}} method without allocating 
(and without exposing the underlying map).

> (GC) Avoid allocating temporary objects in MapMessage
> -----------------------------------------------------
>
>                 Key: LOG4J2-1683
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1683
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: API
>    Affects Versions: 2.7
>            Reporter: Remko Popma
>
> Make MapMessage garbage-free.
> While working on LOG4J2-1677, I found that MapFilter itself can avoid 
> allocating temporary objects, but when filtering a MapMessage, the message 
> allocates a java/util/Collections$UnmodifiableMap:
> {code}
>       at java.util.Collections.unmodifiableMap(Collections.java:1318)
>       at 
> org.apache.logging.log4j.message.MapMessage.getData(MapMessage.java:102)
>       at 
> org.apache.logging.log4j.core.filter.MapFilter.filter(MapFilter.java:70)
> {code}
> Apart from the filtering, logging a MapMessage allocates a StringBuilder and 
> an iterator over the TreeMap:
> {code}
>       at java.lang.StringBuilder.<init>(StringBuilder.java:85)
>       at 
> org.apache.logging.log4j.message.MapMessage.asString(MapMessage.java:189)
>       at 
> org.apache.logging.log4j.message.MapMessage.asString(MapMessage.java:172)
>       at 
> org.apache.logging.log4j.message.MapMessage.getFormattedMessage(MapMessage.java:229)
>       at 
> org.apache.logging.log4j.core.async.RingBufferLogEvent.setMessage(RingBufferLogEvent.java:127)
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to