commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Martin (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-453) ToStringBuilder can cause serious memory leaks
Date Mon, 27 Jul 2009 17:50:15 GMT

    [ https://issues.apache.org/jira/browse/LANG-453?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12735682#action_12735682
] 

Paul Martin commented on LANG-453:
----------------------------------

Can this be reopened?  Or should I create a new issue (with a more detailed explanation and
code sample)?

In summary: I've just seen a memory leak caused by the fact that some values passed to the
ToStringBuilder were both lazily calculated and used by hashCode and equals (meaning that
the hashCode changes during the ToStringBuilder call).  This means that ToStringBuilder (actually
ToStringStyle) is not able to remove the object from the ThreadLocal (since the retrieval
of the object's property value has changed the object's hashCode), thus leaking memory.

Strictly speaking, a mutable object can change its hashCode if its values change, and the
documentation for ToStringBuilder does not require that the object not change in this way.

I've got a few suggestions:
- Don't use a ThreadLocal - just pass around a Map directly.
- Always clear the ThreadLocal at the end of the call.
- Use an IdentityHashMap (though this would not catch dynamically-generated proxied references
to the same object)
- Document restrictions on the objects that can be passed to ToStringBuilder.

Changing the code would also fix otherwise innocuous memory leaks caused by broken equals
and hashCode methods.

I would be happy to provide more information or to work on a patch if required.

> ToStringBuilder can cause serious memory leaks
> ----------------------------------------------
>
>                 Key: LANG-453
>                 URL: https://issues.apache.org/jira/browse/LANG-453
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.3
>         Environment: all
>            Reporter: Olaf Tomczak
>
> ToStringBuilder's constructor registers object passed as argument in thread local static
registry.  This causes serious memory problems in applications that run for a long time, use
thread pooling and process large amount of data (e.g. enterprise web applications). Each call
to toString method on an object causes this object to be practically impossible to garbage
collect.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message