简体   繁体   English

java vm:如何记录 class 卸载

[英]java vm: how to log class unloading

I could not find here on SO and via web search working way for usage of parameters for Java VM (JVM) to log class unloading.我在这里找不到 SO 和通过 web 搜索工作方式,以使用 Java VM(JVM)的参数来记录 class 卸载。

Here http://www.herongyang.com/JVM/ClassLoader-JVM-Option-verbose-class.html advised to call java -verbose:class -version , but it is said for loading and it gave log for loading only. Here http://www.herongyang.com/JVM/ClassLoader-JVM-Option-verbose-class.html advised to call java -verbose:class -version , but it is said for loading and it gave log for loading only.

On Java HotSpot VM Options :Java 热点虚拟机选项上:

-XX:-TraceClassUnloading Trace unloading of classes. -XX:-TraceClassUnloading 跟踪类的卸载。 -XX:-TraceClassLoading Trace loading of classes. -XX:-TraceClassLoading 跟踪类的加载。

java -XX:-TraceClassUnloading -version ouputs no info on classes: java -XX:-TraceClassUnloading -version输出没有关于类的信息:

[0.004s][warning][arguments] -XX:-TraceClassUnloading is deprecated. Will use -Xlog:class+unload=off instead.
java version "13" 2019-09-17
Java(TM) SE Runtime Environment (build 13+33)
Java HotSpot(TM) 64-Bit Server VM (build 13+33, mixed mode, sharing)

and:和:

java -XX:-TraceClassLoading -version
[0.002s][warning][arguments] -XX:-TraceClassLoading is deprecated. Will use -Xlog:class+load=off instead.
java version "13" 2019-09-17
Java(TM) SE Runtime Environment (build 13+33)
Java HotSpot(TM) 64-Bit Server VM (build 13+33, mixed mode, sharing)

Same thing.一样。

Side question: deprecated word means discouraged, not no-longer-working, why such behaviour?附带问题:不推荐使用的词意味着气馁,而不是不再工作,为什么会有这种行为?

however for class loading java -Xlog:class+load -version outputs info eg但是对于 class 加载java -Xlog:class+load -version输出信息例如

[0.037s][info][class,load] java.nio.CharBuffer source: shared objects file
[0.037s][info][class,load] java.nio.HeapCharBuffer source: shared objects file
[0.037s][info][class,load] java.nio.charset.CoderResult source: shared objects file

But java -Xlog:class+unload -version only version info.但是java -Xlog:class+unload -version只有版本信息。

How can I get class unloading log for Java VM HotSpot (and/or OpenJDK)?如何获取 Java VM HotSpot(和/或 OpenJDK)的 class 卸载日志?

You say that你这么说

java -XX:-TraceClassUnloading -version 

outputs no information about classes.不输出关于类的信息。 But I don't know why would expect it to provide information.但我不知道为什么会期望它提供信息。

You would only see logging if the JVM was started AND classes were loaded and unloaded:如果 JVM 已启动并且类已加载和卸载,您只会看到日志记录:

  • The documentation for the -version option says: -version选项的文档说:

    -version Displays version information and then exits. -version显示版本信息然后退出。

    It doesn't say that a JVM is started.并不是说启动了 JVM。

  • Assuming that a JVM is started, and classes are loaded, classes are not unloaded in a normal Java application.假设启动 JVM 并加载类,则在正常的 Java 应用程序中不会卸载类。 Class unloading only occurs happens if: Class 卸载仅在以下情况下发生:

    1. an additional classloader is created, and创建了一个额外的类加载器,并且
    2. that classloader is used to load classes, and该类加载器用于加载类,并且
    3. those classes and classloader all become unreachable 1 , and这些类和类加载器变得无法访问1 ,并且
    4. the GC runs 2 , and GC 运行2 ,并且
    5. the GC detects that the classes are unreachable 2 and unloads 4 them. GC 检测到类不可访问2并卸载4个。

In short you are (were) most likely not seeing any unload log message because no classes are (were) being unloaded.简而言之,您(曾经)很可能没有看到任何卸载日志消息,因为没有(正在)卸载任何类。


1 - There are two-way links between a class and its classloader, and a one-way link from any instance of a class to the class itself. 1 - class 与其类加载器之间有双向链接,从 class 的任何实例到 class 本身的单向链接。 One consequence is that the bootstrap classloader and application classloader will remain reachable for the lifetime of the JVM.结果之一是引导类加载器和应用程序类加载器将在 JVM 的生命周期内保持可访问。

2 - The GC is normally only run when necessary; 2 - GC 通常只在必要时运行; ie when the heap utilization hits a certain threshold.即当堆利用率达到某个阈值时。 It is not run automatically as the JVM exits.当 JVM 退出时,它不会自动运行。

3 - A single run of the GC won't necessarily find all of the unreachable objects. 3 - GC 的单次运行不一定会找到所有无法访问的对象。

4 - Some JVMs / GC's do not support class unloading, and this feature can be disabled (or may need to be enabled) via an command line option. 4 - 一些 JVM / GC 不支持 class 卸载,并且可以通过命令行选项禁用(或可能需要启用)此功能。

If anyone is looking for how to log class unloading?如果有人正在寻找如何记录 class 卸载?

Add JVM argument -Xlog:class+load=info添加JVM 参数-Xlog:class+load=info

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM