JVM为什么为同一个类发送多个JVMTI ClassLoad事件?

时间:2018-08-10 14:29:53

标签: java jvm classloader jvmti

我启用了JVMTI ClassLoad事件,该事件应该在首次加载类时生成。我希望每个加载的类仅一次获得一次此事件,但对于某些类来说似乎会多次生成,例如我得到了两次:

ClassLoad: Ljava/util/concurrent/ThreadFactory; loaded by thread 1
ClassLoad: Ljava/util/concurrent/ThreadFactory; loaded by thread 1

systemDictionary.cpp中,我发现JvmtiExport::post_class_load的三个事件是我负责执行回调的代码(如果有)。他们在

  1. SystemDictionary::resolve_instance_class_or_null
  2. SystemDictionary::parse_stream
  3. SystemDictionary::define_instance_class

但是我还不了解整个流程,因此不清楚为什么我多次获得该赛事。

我有什么办法可以防止这种情况发生?还是需要自己照顾一下并调整代码以忽略同一类的多个事件?

1 个答案:

答案 0 :(得分:0)

可以两次看到该事件。这意味着类解析在不同的类加载器的上下文中发生两次。首先,它由Bootstrap类加载器解决,然后由系统类加载器-sun.misc.Launcher.AppClassLoader解决。

this answer中的实用程序将阐明发生此类事件的情况。

考虑一个简单的例子:

public class Test {

    public void setThreadFactory(ThreadFactory factory) {
    }

    public static void main(String[] args) {
    }
}

启动器寻找public static void main()时,它将为公共方法创建Method对象,这会导致此类方法的签名中的所有类都得到解析。

第一个ClassLoad事件:

Class loaded: java/util/concurrent/ThreadFactory
  - ClassLoad(_jvmtiEnv*, JNIEnv_*, _jobject*, _jclass*) + 0x69
  - JvmtiExport::post_class_load(JavaThread*, Klass*) + 0x15b
  - SystemDictionary::define_instance_class(instanceKlassHandle, Thread*) + 0x3cc
  - SystemDictionary::find_or_define_instance_class(Symbol*, Handle, instanceKlassHandle, Thread*) + 0x35d
  - SystemDictionary::load_instance_class(Symbol*, Handle, Thread*) + 0x20c
  - SystemDictionary::resolve_instance_class_or_null(Symbol*, Handle, Handle, Thread*) + 0x78c
  - JVM_FindClassFromBootLoader + 0x22b
  - Java_java_lang_ClassLoader_findBootstrapClass + 0x9b
  * java/lang/ClassLoader.findBootstrapClass @ -1
  * java/lang/ClassLoader.findBootstrapClassOrNull @ 12
  * java/lang/ClassLoader.loadClass @ 48
  * java/lang/ClassLoader.loadClass @ 38
  * sun/misc/Launcher$AppClassLoader.loadClass @ 81
  * java/lang/ClassLoader.loadClass @ 3
  * java/lang/Class.getDeclaredMethods0 @ -1
  * java/lang/Class.privateGetDeclaredMethods @ 37
  * java/lang/Class.privateGetMethodRecursive @ 2
  * java/lang/Class.getMethod0 @ 16
  * java/lang/Class.getMethod @ 13
  * sun/launcher/LauncherHelper.validateMainClass @ 12
  * sun/launcher/LauncherHelper.checkAndLoadMain @ 214

第二个:

Class loaded: java/util/concurrent/ThreadFactory
  - ClassLoad(_jvmtiEnv*, JNIEnv_*, _jobject*, _jclass*) + 0x69
  - JvmtiExport::post_class_load(JavaThread*, Klass*) + 0x15b
  - SystemDictionary::resolve_instance_class_or_null(Symbol*, Handle, Handle, Thread*) + 0x87c
  - SystemDictionary::resolve_or_fail(Symbol*, Handle, Handle, bool, Thread*) + 0x33
  - get_mirror_from_signature(methodHandle, SignatureStream*, Thread*) + 0xc6
  - Reflection::get_parameter_types(methodHandle, int, oopDesc**, Thread*) + 0x18e
  - Reflection::new_method(methodHandle, bool, bool, Thread*) + 0xfc
  - get_class_declared_methods_helper(JNIEnv_*, _jclass*, unsigned char, bool, Klass*, Thread*) + 0x479
  - JVM_GetClassDeclaredMethods + 0xcb
  * java/lang/Class.getDeclaredMethods0 @ -1
  * java/lang/Class.privateGetDeclaredMethods @ 37
  * java/lang/Class.privateGetMethodRecursive @ 2
  * java/lang/Class.getMethod0 @ 16
  * java/lang/Class.getMethod @ 13
  * sun/launcher/LauncherHelper.validateMainClass @ 12
  * sun/launcher/LauncherHelper.checkAndLoadMain @ 214