简体   繁体   中英

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 eg

[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)?

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.

  • 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 unreachable 1 , and
    4. the GC runs 2 , and
    5. the GC detects that the classes are unreachable 2 and unloads 4 them.

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. 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; ie 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.

If anyone is looking for how to log class unloading?

Add JVM argument -Xlog:class+load=info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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