java 如何跟踪类何时在 jvm 中加载和销毁?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9921081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to track when class is loaded and destroyed in jvm?
提问by Jyotirup
How do I keep track when class is loaded and destroyed in jvm? Is there any callback method that is exposed by the jvm?
在 jvm 中加载和销毁类时如何跟踪?jvm 是否有任何回调方法?
回答by Dan Cruz
If you're using a Sun/Oracle JVM, you could use the TraceClassLoading
and TraceClassUnloading
options. Use the following to see what options your JVM supports:
如果您使用的是 Sun/Oracle JVM,则可以使用TraceClassLoading
和TraceClassUnloading
选项。使用以下命令查看您的 JVM 支持哪些选项:
java -XX:+AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal -version
If these options are supported, run your Java application using -XX:+TraceClassLoading -XX:+TraceClassUnloading
. You should see messages like:
如果支持这些选项,请使用-XX:+TraceClassLoading -XX:+TraceClassUnloading
. 您应该会看到如下消息:
[Loaded ... from ...]
[Unloading class ...]
回答by rsp
You can add the command line option-verbose:class
to your Java process, this will
display information about each class loaded.
您可以将命令行选项添加-verbose:class
到 Java 进程中,这将显示有关加载的每个类的信息。
回答by Pau Kiat Wee
You can add Java Opts to see which class is loaded via:
您可以添加 Java Opts 以查看通过以下方式加载的类:
java -verbose:class
About destroyed class, I am not sure.
关于销毁的类,我不确定。
回答by NiranjanBhat
If are ok with using JRockit JVM, you can make use of the APIs below, which will give you callback methods when a class is loaded and class is unloaded.
Have a look at JVMclass from which we are supposed to use the getClassLibrary()
method.
On the classLibrary object we can register listeners for classloading events which gives the class names etc.
如果可以使用JRockit JVM,您可以使用下面的 API,它们将在加载类和卸载类时为您提供回调方法。
看看我们应该从中使用该方法的JVM类getClassLibrary()
。
在 classLibrary 对象上,我们可以为类加载事件注册监听器,这些事件提供类名等。
回答by Luca
You can use static block to detect class loading but you can not detect class unloading. In java all classes loaded through the system classloader will never be unloaded and all classes loaded through other classloaders will be unloaded only when the classloader is unloaded.
您可以使用静态块来检测类加载,但不能检测类卸载。在java中,所有通过系统类加载器加载的类永远不会被卸载,所有通过其他类加载器加载的类只有在类加载器被卸载时才会被卸载。
static{
//execute when the class will be loaded
}
回答by Kai
Do you want this information in your application or do you just want to analyse that from the outside? In the latter case you maybe can use VisualVMfor that. Maybe your question is related to this one: Loaded classes in VisualVM.
您希望在您的应用程序中使用这些信息,还是只想从外部对其进行分析?在后一种情况下,您可能可以使用VisualVM。也许您的问题与此有关:在 VisualVM 中加载类。
回答by Sergey A. Savenko
You can track class's creation in static constructor. And you cannot track it's destruction as far as I'm concerned. Classes are only unloaded when gc collects the classloader which was used to load the classes.
You might also be interested in reading this: another question on classloaders at StackOverflow
您可以在静态构造函数中跟踪类的创建。就我而言,你无法追踪它的破坏情况。仅当 gc 收集用于加载类的类加载器时才会卸载类。
您可能也有兴趣阅读:关于 StackOverflow 上的类加载器的另一个问题
回答by Jorn Vernee
From Java 9 onward, you can use -Xlog
. e.g.:
从 Java 9 开始,您可以使用-Xlog
. 例如:
java -Xlog:class+load -Xlog:class+unload ...
This will print entries like:
这将打印如下条目:
[0.296s][info][class,load] java.lang.Shutdown source: jrt:/java.base
[0.296s][info][class,load] java.lang.Shutdown$Lock source: jrt:/java.base
You can also use -Xlog:help
to get more information on the option.
您还可以使用-Xlog:help
来获取有关该选项的更多信息。