本文介绍了使用不同的类加载器加载类,以便在不需要时从JVM卸载它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在使用ServiceLoader加载具有不同ClassLoader的模块(.jar文件中的类),以便在不需要时从应用程序上下文和JVM本身中完全卸载它们.我知道从JVM卸载类"可能不是一个常见的话题,有一些条件可以实现,所以我正在做出必要的努力.为了确保所有功能都按预期运行,我正在使用-XX:+ TraceClassLoading和-XX:+ TraceClassUnloading跟踪类的加载和卸载,此参数的信息显示了我可以从以下位置完全卸载任何模块mi应用程序以及从JVM本身(从JVM卸载是在完整的GC期间进行的).所有这些似乎都可以正常工作...但是我的问题是,为什么从中加载模块类的.jar文件会被JVM保持打开状态?无法删除.jars,但是JVM表示已从中卸载了这些类. .显然,这是在JVM执行期间,但是如果JVM没有从中加载类,为什么还要打开这些.jar文件?我有什么办法可以迫使JVM释放这些.jar文件?

In my application i'm using ServiceLoader to load modules(classes from .jar file) with different ClassLoaders in order to completely unload them when not needed from the context of the application and from the JVM itself. I know maybe is not a common topic "unload classes from JVM", there are some conditions for that to happen, so i am making the necessary effort. To make sure if all is working like expected i'm tracing the load and unload of classes with -XX:+TraceClassLoading and -XX:+TraceClassUnloading, the info from this parameters shows me that i can do a complete unload of any module from mi app and from the JVM itself (unload from the JVM happens during a complete GC). All this seems to work fine... but my question is why the .jar files from where the module classes was loaded are keep open by the JVM?, that .jars cant be removed but the JVM says to have unloaded the classes from them. Obviously this is during JVM execution, but why keep open that .jar files if the JVM has no class loaded from them?, there is any thing i can do to force the JVM to free that .jar files?

推荐答案

应该在不同的Web容器内深入研究类加载/卸载的问题.

Issue of the class load / unload should be deeply explored within different web containers.

一些搜索显示Tomcat 6.x具有选项antiJAR锁定该选项在 http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/java/org/apache/catalina /loader/

Some googling shows up that Tomcat 6.x has option antiJARLocking that option is used in Tomcat sources at http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/java/org/apache/catalina/loader/

请阅读源代码,您将在那里获得加载/卸载代码的示例.

Please read the sources and you will get sample of load / unload code there.

这篇关于使用不同的类加载器加载类,以便在不需要时从JVM卸载它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 06:46