Preston Preston - 1 year ago 86
Java Question

Do I have a JAXB classloader leak

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 added the following to my jvm arguments.

Now when watching the output, I see the same classes being loaded over and over again. Basically every time a web service is called and JAXB is used to process the xml.

[Loaded com.strikeiron.ZIPCodesInRadius$JaxbAccessorF_userID from JVM_DefineClass]
[Loaded com.strikeiron.ZIPCodesInRadius$JaxbAccessorF_userID from JVM_DefineClass]

Does this indicate a leak? If so how do I resolve it?

Answer Source

I found a similar thread that was describing the same problem I was having.

I also found a bug at

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 {

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download