How to track when class is loaded and destroyed in jvm?

后端 未结 8 1220
醉梦人生
醉梦人生 2020-12-24 02:25

How do I keep track when class is loaded and destroyed in jvm? Is there any callback method that is exposed by the jvm?

相关标签:
8条回答
  • 2020-12-24 03:26

    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

    0 讨论(0)
  • 2020-12-24 03:30

    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:

    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:

    [Loaded ... from ...]
    [Unloading class ...]
    
    0 讨论(0)
提交回复
热议问题