Old JaxB and JDK8 Metaspace OutOfMemory Issue

16,180

Solution 1

We solved our current issue untill able to fix all occurances in our application by using the following VM-parameter:

-Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true

I hope this will help others with similar issues...

Solution 2

JAXBContext.newInstance() should be used once to create a context for your class to unmarshall. It will use up your permgen or metaspace otherwise.

Solution 3

Here is the solution Gary is talking about, which is better than just setting a flag (since even the JAXB guys suggest to make it singleton...)

private static Map<class<?>, JAXBContext> contextStore = new ConcurrentHashMap<class<?>, JAXBContext>();
... 
protected static JAXBContext getContextInstance(Class<?> objectClass) throws JAXBException{
  JAXBContext context = contextStore.get(objectClass);
  if (context==null){
    context = JAXBContext.newInstance(objectClass);
    contextStore.put(objectClass, context);
  }
  return context;
}

//using it like this:
JAXBContext context = getContextInstance(objectClass);

The JAXB-564 Bug + fix could be found here. Inspired by the long gone blog of scorgar

Share:
16,180
The_Gentleman
Author by

The_Gentleman

Senior Software Developer / Senior Anwendungsentwickler DevOps Master of Science Applied computer science / Master of Science in Angewandter Informatik Information systems technician / Informations- und Telekommunikations-System-Elektroniker

Updated on June 09, 2022

Comments

  • The_Gentleman
    The_Gentleman almost 2 years

    We are working on a business application (1 million+ LOC) developed since 10+ years. While switching to JDK8 we get an issue with the metaspace of JDK8. This seems to be related to the JaxB-Version referenced in com.sun.xml.ws:webservices-rt:1.4 (Metro 1.4). Because of the intense linking in the application and legacy creation of classes/instances via JaxB it isn't simple to switch on the fly the old libraries.

    Currently we are researching this issue. We created a sample programm that reproduces this behavior:

    import java.io.ByteArrayInputStream;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class X
    {
      private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><x test=\"test\" />";
    
      @XmlAttribute
      String test;
    
      public static void main( String[] args ) throws JAXBException, InterruptedException
      {
        System.out.println("start");
    
        while ( true )
        {
          JAXBContext jc = JAXBContext.newInstance( X.class );
          Unmarshaller unmarshaller = jc.createUnmarshaller();
          X object = (X) unmarshaller.unmarshal( new ByteArrayInputStream( XML.getBytes() ) );
          System.out.println( object.test );
        }
      }
    }
    

    JDK7 keeps the PermGenSpace clean. (Simulated with 16M PermGen) Memory of run with JDK7

    Using JDK8 the application runs slowly to the OOM exception. The VisualVM catches the exception and keeps the process running on the maximum of available Metaspace. Even here it gets stucked after quite a while running on max. (Simulated with 16M Metaspace) Memory of run with JDK8

    Has anyone some ideas how to get the garbage collectors legacy behavior, so we don't run into those out of memory issues? Or do you have any other ideas how to deal with this issue?

    Thanks.

    edit1: Run parameters JDK7:

    -XX:+TraceClassLoading -XX:+TraceClassUnloading -XX:MaxPermSize=16M -XX:PermSize=1M -XX:+UseParallelOldGC -XX:+HeapDumpOnOutOfMemoryError

    => No heap dumps are created

    Run parameters JDK8:

    -XX:+TraceClassLoading -XX:+TraceClassUnloading -XX:MaxMetaspaceSize=16M -XX:MetaspaceSize=1M -XX:+UseParallelOldGC -XX:+HeapDumpOnOutOfMemoryError

    => heap dumps are generated while running.

    The Memory available of VisualVM does not show the real maximum metaspace value. If not limited the metaspace is constantly increasing untill memory is exceeded.

    edit 2:

    I have tried all available garbage collectors for JDK8. They all have the same issue.

    edit 3:

    Solving by exchanging the libs is difficult in our real application because of heavy coupling between JAXB & several modules of our application. So a fix for the garbage collector behavior is needed for the short run. On the long run the propper fix is already planned.