[
https://issues.apache.org/jira/browse/CXF-7710?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16450366#comment-16450366
]
ASF GitHub Bot commented on CXF-7710:
-------------------------------------
dkulp commented on issue #409: CXF-7710: ClientImpl is memory-leak prone
URL: https://github.com/apache/cxf/pull/409#issuecomment-384036673
Nothing about this change will actually work. Changing to a
WeakHashMap<String, ....> and using a "new String(...)" for the key will result
in nothing actually holding onto the key and the context could be garbage
collected immediately. The test case you added
(requestContextIsGarbageCollected) shows the exact problem. Two calls to
getRequestContext() on the same thread should return the same context. It's
very common to do:
```
client.getRequestContext().put("A", aValue);
client.getRequestContext().put("B", bValue);
client.getRequestContext().put("C", cValue);
```
and that NEEDS to work. If something triggers a GC between there, values
will get lost.
As part of CXF-7591, we made sure the getResponseContext().clear() will
work so that users can explicitly clear the context when done with it to avoid
this problem.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> ClientImpl is memory-leak prone
> -------------------------------
>
> Key: CXF-7710
> URL: https://issues.apache.org/jira/browse/CXF-7710
> Project: CXF
> Issue Type: Bug
> Reporter: Facundo Velazquez
> Priority: Critical
> Attachments: heapdump_Leak_Suspects.zip, leak capture.png
>
>
> In the Mule ESB we are seeing a memory leak caused by non-released objects in
> the
> org.apache.cxf.endpoint.ClientImpl.
> After some research, I could see that the requestContext and the
> responseContext have a thread local implementation. As our code calls the
> client from different threads, in a high load scenario, lots of entries will
> be put in the requestContext map. Take into account that we clean each
> requestContext value (that is an EchoContext object), but an entry per thread
> is kept alive in the requestContext map (with an empty EchoContext map).
> You'll able to see in the attached files that this is causing a memory leak.
> Even in my tests trying to reproduce the issue, I've obtained a fatal
> OutOfMemoryError.
> Looking at the code, I've seen that the request context is a WeakHashMap,
> however the keys are threads. I supposed the purpose of this implementation
> is that entries can be removed when necessary by the garbage collector.
> However, if the threads are pooled (which is our case), strong references
> will be pointing to them, and will be never collected.
> I suppose an easy solution could be to use the thread names as keys instead
> threads objects directly. If this approach is taken, consider using string
> constructors to wrap the literal name for ensuring its garbage collection
> (since this is another well-know issue -->
> [https://stackoverflow.com/questions/14494875/weakreference-string-didnt-garbage-collected-how|https://stackoverflow.com/questions/14494875/weakreference-string-didnt-garbage-collected-how).]
> ).
> Another solution, that entails more changes, would be to use a Guava Cache,
> setting an expiration time.
> If the first approach is implemented, could you provide a way to clean the
> requestContext programmatically?, so in this way, we don't have to depend on
> the garbage collection process.
> Thank you very much.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)