我有 JAXB 类加载器泄漏吗 [英] Do I have a JAXB classloader leak

查看:29
本文介绍了我有 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:+PrintGC详细信息-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.

[从 JVM_DefineClass 加载 com.strikeiron.ZIPCodesInRadius$JaxbAccessorF_userID][从 JVM_DefineClass 加载了 com.strikeiron.ZIPCodesInRadius$JaxbAccessorF_userID]

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

推荐答案

我发现一个类似的帖子描述了我遇到的相同问题.http://forums.java.net/jive/thread.jspa?threadID=53362

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

我还发现了一个错误https://github.com/javaee/jaxb-v2/issues/581

基本上,问题是每次调用我的 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 类加载器泄漏吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆