Search code examples
javajvmmonitoringjvm-hotspot

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.

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:

-XX:-TraceClassUnloading Trace unloading of classes. -XX:-TraceClassLoading Trace loading of classes.

java -XX:-TraceClassUnloading -version ouputs no info on classes:

[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 e.g.

[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.

How can I get class unloading log for Java VM HotSpot (and/or OpenJDK)?


Solution

  • 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:

    • The documentation for the -version option says:

      -version Displays version information and then exits.

      It doesn't say that a JVM is started.

      Remove the -version/

    • As of Java 9, -XX:-TraceClassUnloading is deprecated. You can replace it with -Xlog:class+load=info.

    • Assuming that a JVM is started, and classes are loaded, classes are not unloaded in a normal Java application. Class unloading only occurs happens if:

      1. an additional classloader is created, and
      2. that classloader is used to load classes, and
      3. those classes and classloader all become unreachable1, and
      4. the GC runs2, and
      5. the GC detects that the classes are unreachable2 and unloads4 them.

    In short you are (were) most likely not seeing any unload log message because no classes are (were) being unloaded.


    Side question: deprecated word means discouraged, not no-longer-working, why such behavior?

    Because they decided to change the way that the JVM logging options worked. The changes happened in Java 9: see https://openjdk.java.net/jeps/158.

    The -XX options are subject to change. Check the manual for the version of Java that you are using for the current options. Changes (feature deprecation and removal) should also be mentioned in the relevant release notes.

    The page on JVM options you linked to says in bold:

    "Please note that this page only applies to JDK 7 and earlier releases."


    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. One consequence is that the bootstrap classloader and application classloader will remain reachable for the lifetime of the JVM.
    2 - The GC is normally only run when necessary; i.e. when the heap utilization hits a certain threshold. It is not run automatically as the JVM exits.
    3 - A single run of the GC won't necessarily find all of the unreachable objects.
    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.