As described in a comment of bug
CXF-2164, there's still a case of memory leak produced by CXF when embedded into a web application and initialized through Spring.
The problem is that cxf.xml declares the following bean:
<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl"/>
Here, the destroy-method is not declared. This means that when Sping shuts down the context, it does not call any shutdown method on the CXFBusImpl.
The net result is the following:
- Tomcat starts
- Tomcat starts my web application
- this declares a Spring web application context that gets parsed
- at some point cxf.xml is parsed, so the bean named cxf is created and an org.apache.cxf.bus.CXFBusImpl instance is created using the constructor org.apache.cxf.bus.CXFBusImpl.CXFBusImpl(Map<Class, Object>), which causes the org.apache.cxf.BusFactory.localBus ThreadLocal to be assigned a value of this type, through a call to org.apache.cxf.BusFactory.possiblySetDefaultBus(Bus); I see that this happens in a thread called "main"
- if I stop the web application, Spring closes down the context; however, as I said before, this does not cause any shutdown method of the cxf bean to be called
- so there's no null-setting or removing operation of the org.apache.cxf.BusFactory.localBus ThreadLocal for the thread "main"
The problem of the use of a ThreadLocal is that even if the shutdown method od CXFBusImpl were called, the shutdown operation may be done in a thread which is different from the one in which the context had been created.
As suggested by Daniel Kulp in the other report, maybe the best solution is to use a WeakHashMap<Thread, Bus> to keep the thread-bus relationships.
I'm going to provide a patch.