本文介绍了我是否有JAXB类加载器泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Glassfish上部署了一个应用程序。随着时间的推移,加载的类数量增加到数百万,我的permgen似乎上升。

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

为了帮助排除故障,我将以下内容添加到我的jvm参数中。
-XX:+ PrintGCDetails
-XX:+ TraceClassUnloading
-XX:+ TraceClassLoading

To help troubleshoot I added the following to my jvm arguments.-XX:+PrintGCDetails-XX:+TraceClassUnloading-XX:+TraceClassLoading

现在看输出时,我看到了一遍又一遍地加载相同的类。基本上每次调用Web服务时都会使用JAXB来处理xml。

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.

[加载com.strikeiron.ZIPCodesInRadius $ JaxbAccessorF_userID来自 JVM_DefineClass ]
[加载com.strikeiron.ZIPCodesInRadius $ JaxbAccessorF_userID来自 JVM_DefineClass ]

[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?

推荐答案

我发现了一个类似的线程,它描述了我遇到的同样问题。

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

我还在
发现了一个错误

基本上,问题我每次调用bean时都在做一个新的JAXBContext(your.class.xsd)。根据错误调用JAXBContext.newInstance(...)意味着重新加载所有内容,因为当前或指定的类加载器将被(重新)使用。

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;
}

}

并使用单身人士

JAXBContext context = JAXBContextSingleton.INSTANCE.getContext();

这篇关于我是否有JAXB类加载器泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:54