我是否存在 JAXB 类加载器泄漏

发布于 2024-09-10 01:55:02 字数 459 浏览 1 评论 0原文

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

为了帮助排除故障,我将以下内容添加到我的 jvm 参数中。 -XX:+打印GC详细信息 -XX:+TraceClassUnloading -XX:+TraceClassLoading

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

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

这是否表明存在泄漏?如果是这样我该如何解决?

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.
-XX:+PrintGCDetails
-XX:+TraceClassUnloading
-XX:+TraceClassLoading

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-09-17 01:55:02

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

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

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

解决方案是创建一个效果很好的单例。

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();

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

I also found a bug at
https://github.com/javaee/jaxb-v2/issues/581

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

}

And to use the singleton

JAXBContext context = JAXBContextSingleton.INSTANCE.getContext();
恋你朝朝暮暮 2024-09-17 01:55:02

这也是我远离JAXB的原因之一。我宁愿编写分别实现 javax.xml.bind.Marshaller 和 javax.xml.bindUnmarshaller 的类来编组和解组。我写一次就完成了。没有任何反思和动态类生成。

This is one of the reasons why I stay away from JAXB. I'd rather write classes to marshal and unmarshal that implement javax.xml.bind.Marshaller and javax.xml.bindUnmarshaller, respectively. I write them once and they're done. None of that reflection and dynamic class generation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文