我是否存在 JAXB 类加载器泄漏
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现一个类似的线程描述了我遇到的同样的问题。
http://forums.java.net/jive/thread.jspa?threadID= 53362
我还发现了一个错误
https://github.com/javaee/jaxb-v2/issues/581
基本上,问题是每次调用我的 bean 时我都会执行一个新的 JAXBContext("your.class.xsd") 。根据错误“调用 JAXBContext.newInstance(...) 意味着重新加载所有内容,因为要(重新)使用当前或指定的类加载器。”
解决方案是创建一个效果很好的单例。
并使用单例
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.
And to use the singleton
这也是我远离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
andjavax.xml.bindUnmarshaller
, respectively. I write them once and they're done. None of that reflection and dynamic class generation.