I have an application in Java that uses CXF to make calls to a web service. The code for the initiator looks like
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.tempuri.IAPIService;
import javax.inject.Inject;
/**
* Created by flavius on 23/09/14.
*/
@Component()
public class VsJaxWsProxyFactory {
@Inject
private Environment env;
private JaxWsProxyFactoryBean factoryBean = null;
public Object create() {
if (factoryBean == null) {
factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(IAPIService.class);
factoryBean.setAddress(env.getProperty("api.wsdl"));
}
return factoryBean.create();
}
}
The application runs fine, but after a while the box starts to consume too much memory and the application starts running slow. When we tried to do a memory analysis we found the following

It seems that the CXF is somehow creating a new Service configuration object on each call, and is not being released. I can't find much information on org.apache.cxf.jaxws.support.JaxWsServiceConfiguration class and not sure if this is a configuration issue or some internal bug in some library.
Already answered here: Apache CXF not releasing clients
Basically .create() method on the same JaxWsProxyFactoryBean instance should be called only once, otherwise you get a memory leak:
return factoryBean.create();
factoryBean.create() returns the cxf JAX-WS client proxy, which you can reuse for repeated calls. Created client is also thread safe if you don't use any special features mentioned here http://cxf.apache.org/faq.html#FAQ-AreJAX-WSclientproxiesthreadsafe?