Old JaxB and JDK8 Metaspace OutOfMemory Issue

前端 未结 3 508
陌清茗
陌清茗 2020-12-02 20:56

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 rel

3条回答
  •  离开以前
    2020-12-02 21:19

    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, JAXBContext> contextStore = new ConcurrentHashMap, 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);
    

提交回复
热议问题