Do I have a JAXB classloader leak

前端 未结 2 2091
失恋的感觉
失恋的感觉 2020-12-06 17:44

I have an application deployed on Glassfish. Over time the number of loaded classes climbs into the millions and my permgen seems to rise.

To help troubleshoot I add

相关标签:
2条回答
  • 2020-12-06 18:03

    This is one of the reasons why I stay away from JAXB. I'd rather write classes to marshal and unmarshal that implement javax.xml.bind.Marshaller and javax.xml.bindUnmarshaller, respectively. I write them once and they're done. None of that reflection and dynamic class generation.

    0 讨论(0)
  • 2020-12-06 18:14

    I found a similar thread that was describing the same problem I was having. http://forums.java.net/jive/thread.jspa?threadID=53362

    I also found a bug at https://github.com/javaee/jaxb-v2/issues/581

    Basically, the problem was that I was doing a new JAXBContext("your.class.xsd") every time my bean was invoked. According to the bug "Calling JAXBContext.newInstance(...) implies reloading of everything since either the current or the specified class loader is to be (re-)used."

    The solution was to create a singleton which worked great.

    public enum JAXBContextSingleton {
    
    INSTANCE("your.class.xsd");
    private JAXBContext context;
    
    JAXBContextSingleton(String classToCreate) {
        try {
            this.context = JAXBContext.newInstance(classToCreate);
        } catch (JAXBException ex) {
            throw new IllegalStateException("Unbale to create JAXBContextSingleton");
        }
    }
    
    public JAXBContext getContext(){
        return context;
    }
    
    }
    

    And to use the singleton

    JAXBContext context = JAXBContextSingleton.INSTANCE.getContext();
    
    0 讨论(0)
提交回复
热议问题