49 #include "precompiled.hpp"
50 #include "classfile/classLoaderData.inline.hpp"
51 #include "classfile/classLoaderDataGraph.inline.hpp"
52 #include "classfile/dictionary.hpp"
53 #include "classfile/javaClasses.inline.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "classfile/systemDictionaryShared.hpp"
59 #include "classfile/vmClasses.hpp"
60 #include "logging/log.hpp"
61 #include "logging/logStream.hpp"
62 #include "memory/allocation.inline.hpp"
63 #include "memory/classLoaderMetaspace.hpp"
64 #include "memory/metadataFactory.hpp"
65 #include "memory/metaspace.hpp"
66 #include "memory/resourceArea.hpp"
67 #include "memory/universe.hpp"
68 #include "oops/access.inline.hpp"
69 #include "oops/klass.inline.hpp"
70 #include "oops/oop.inline.hpp"
71 #include "oops/oopHandle.inline.hpp"
72 #include "oops/verifyOopClosure.hpp"
73 #include "oops/weakHandle.inline.hpp"
74 #include "runtime/arguments.hpp"
75 #include "runtime/atomic.hpp"
76 #include "runtime/handles.inline.hpp"
77 #include "runtime/mutex.hpp"
78 #include "runtime/safepoint.hpp"
79 #include "utilities/growableArray.hpp"
80 #include "utilities/macros.hpp"
81 #include "utilities/ostream.hpp"
82
83 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = nullptr;
84
85 void ClassLoaderData::init_null_class_loader_data() {
86 assert(_the_null_class_loader_data == nullptr, "cannot initialize twice");
87 assert(ClassLoaderDataGraph::_head == nullptr, "cannot initialize twice");
88
427
428 #ifdef ASSERT
429 oop m = k->java_mirror();
430 assert(m != nullptr, "nullptr mirror");
431 assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
432 #endif
433 klass_closure->do_klass(k);
434 }
435 }
436
437 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
438 // Lock-free access requires load_acquire
439 for (Klass* k = Atomic::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
440 if (k->is_instance_klass()) {
441 f(InstanceKlass::cast(k));
442 }
443 assert(k != k->next_link(), "no loops!");
444 }
445 }
446
447 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
448 assert_locked_or_safepoint(Module_lock);
449 if (_unnamed_module != nullptr) {
450 f(_unnamed_module);
451 }
452 if (_modules != nullptr) {
453 _modules->modules_do(f);
454 }
455 }
456
457 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
458 assert_locked_or_safepoint(Module_lock);
459 if (_packages != nullptr) {
460 _packages->packages_do(f);
461 }
462 }
463
464 void ClassLoaderData::record_dependency(const Klass* k) {
465 assert(k != nullptr, "invariant");
466
578 }
579 ShouldNotReachHere(); // should have found this class!!
580 }
581
582 void ClassLoaderData::unload() {
583 _unloading = true;
584
585 LogTarget(Trace, class, loader, data) lt;
586 if (lt.is_enabled()) {
587 ResourceMark rm;
588 LogStream ls(lt);
589 ls.print("unload");
590 print_value_on(&ls);
591 ls.cr();
592 }
593
594 // Some items on the _deallocate_list need to free their C heap structures
595 // if they are not already on the _klasses list.
596 free_deallocate_list_C_heap_structures();
597
598 // Clean up class dependencies and tell serviceability tools
599 // these classes are unloading. This must be called
600 // after erroneous classes are released.
601 classes_do(InstanceKlass::unload_class);
602
603 // Method::clear_jmethod_ids only sets the jmethod_ids to null without
604 // releasing the memory for related JNIMethodBlocks and JNIMethodBlockNodes.
605 // This is done intentionally because native code (e.g. JVMTI agent) holding
606 // jmethod_ids may access them after the associated classes and class loader
607 // are unloaded. The Java Native Interface Specification says "method ID
608 // does not prevent the VM from unloading the class from which the ID has
609 // been derived. After the class is unloaded, the method or field ID becomes
610 // invalid". In real world usages, the native code may rely on jmethod_ids
611 // being null after class unloading. Hence, it is unsafe to free the memory
612 // from the VM side without knowing when native code is going to stop using
613 // them.
614 if (_jmethod_ids != nullptr) {
615 Method::clear_jmethod_ids(this);
616 }
617 }
872 void ClassLoaderData::free_deallocate_list() {
873 // This must be called at a safepoint because it depends on metadata walking at
874 // safepoint cleanup time.
875 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
876 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
877 if (_deallocate_list == nullptr) {
878 return;
879 }
880 // Go backwards because this removes entries that are freed.
881 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
882 Metadata* m = _deallocate_list->at(i);
883 if (!m->on_stack()) {
884 _deallocate_list->remove_at(i);
885 // There are only three types of metadata that we deallocate directly.
886 // Cast them so they can be used by the template function.
887 if (m->is_method()) {
888 MetadataFactory::free_metadata(this, (Method*)m);
889 } else if (m->is_constantPool()) {
890 MetadataFactory::free_metadata(this, (ConstantPool*)m);
891 } else if (m->is_klass()) {
892 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
893 } else {
894 ShouldNotReachHere();
895 }
896 } else {
897 // Metadata is alive.
898 // If scratch_class is on stack then it shouldn't be on this list!
899 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
900 "scratch classes on this list should be dead");
901 // Also should assert that other metadata on the list was found in handles.
902 // Some cleaning remains.
903 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
904 }
905 }
906 }
907
908 // This is distinct from free_deallocate_list. For class loader data that are
909 // unloading, this frees the C heap memory for items on the list, and unlinks
910 // scratch or error classes so that unloading events aren't triggered for these
911 // classes. The metadata is removed with the unloading metaspace.
912 // There isn't C heap memory allocated for methods, so nothing is done for them.
|
49 #include "precompiled.hpp"
50 #include "classfile/classLoaderData.inline.hpp"
51 #include "classfile/classLoaderDataGraph.inline.hpp"
52 #include "classfile/dictionary.hpp"
53 #include "classfile/javaClasses.inline.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "classfile/systemDictionaryShared.hpp"
59 #include "classfile/vmClasses.hpp"
60 #include "logging/log.hpp"
61 #include "logging/logStream.hpp"
62 #include "memory/allocation.inline.hpp"
63 #include "memory/classLoaderMetaspace.hpp"
64 #include "memory/metadataFactory.hpp"
65 #include "memory/metaspace.hpp"
66 #include "memory/resourceArea.hpp"
67 #include "memory/universe.hpp"
68 #include "oops/access.inline.hpp"
69 #include "oops/inlineKlass.inline.hpp"
70 #include "oops/klass.inline.hpp"
71 #include "oops/oop.inline.hpp"
72 #include "oops/oopHandle.inline.hpp"
73 #include "oops/verifyOopClosure.hpp"
74 #include "oops/weakHandle.inline.hpp"
75 #include "runtime/arguments.hpp"
76 #include "runtime/atomic.hpp"
77 #include "runtime/handles.inline.hpp"
78 #include "runtime/mutex.hpp"
79 #include "runtime/safepoint.hpp"
80 #include "utilities/growableArray.hpp"
81 #include "utilities/macros.hpp"
82 #include "utilities/ostream.hpp"
83
84 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = nullptr;
85
86 void ClassLoaderData::init_null_class_loader_data() {
87 assert(_the_null_class_loader_data == nullptr, "cannot initialize twice");
88 assert(ClassLoaderDataGraph::_head == nullptr, "cannot initialize twice");
89
428
429 #ifdef ASSERT
430 oop m = k->java_mirror();
431 assert(m != nullptr, "nullptr mirror");
432 assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
433 #endif
434 klass_closure->do_klass(k);
435 }
436 }
437
438 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
439 // Lock-free access requires load_acquire
440 for (Klass* k = Atomic::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
441 if (k->is_instance_klass()) {
442 f(InstanceKlass::cast(k));
443 }
444 assert(k != k->next_link(), "no loops!");
445 }
446 }
447
448 void ClassLoaderData::inline_classes_do(void f(InlineKlass*)) {
449 // Lock-free access requires load_acquire
450 for (Klass* k = Atomic::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
451 if (k->is_inline_klass()) {
452 f(InlineKlass::cast(k));
453 }
454 assert(k != k->next_link(), "no loops!");
455 }
456 }
457
458 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
459 assert_locked_or_safepoint(Module_lock);
460 if (_unnamed_module != nullptr) {
461 f(_unnamed_module);
462 }
463 if (_modules != nullptr) {
464 _modules->modules_do(f);
465 }
466 }
467
468 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
469 assert_locked_or_safepoint(Module_lock);
470 if (_packages != nullptr) {
471 _packages->packages_do(f);
472 }
473 }
474
475 void ClassLoaderData::record_dependency(const Klass* k) {
476 assert(k != nullptr, "invariant");
477
589 }
590 ShouldNotReachHere(); // should have found this class!!
591 }
592
593 void ClassLoaderData::unload() {
594 _unloading = true;
595
596 LogTarget(Trace, class, loader, data) lt;
597 if (lt.is_enabled()) {
598 ResourceMark rm;
599 LogStream ls(lt);
600 ls.print("unload");
601 print_value_on(&ls);
602 ls.cr();
603 }
604
605 // Some items on the _deallocate_list need to free their C heap structures
606 // if they are not already on the _klasses list.
607 free_deallocate_list_C_heap_structures();
608
609 inline_classes_do(InlineKlass::cleanup);
610
611 // Clean up class dependencies and tell serviceability tools
612 // these classes are unloading. This must be called
613 // after erroneous classes are released.
614 classes_do(InstanceKlass::unload_class);
615
616 // Method::clear_jmethod_ids only sets the jmethod_ids to null without
617 // releasing the memory for related JNIMethodBlocks and JNIMethodBlockNodes.
618 // This is done intentionally because native code (e.g. JVMTI agent) holding
619 // jmethod_ids may access them after the associated classes and class loader
620 // are unloaded. The Java Native Interface Specification says "method ID
621 // does not prevent the VM from unloading the class from which the ID has
622 // been derived. After the class is unloaded, the method or field ID becomes
623 // invalid". In real world usages, the native code may rely on jmethod_ids
624 // being null after class unloading. Hence, it is unsafe to free the memory
625 // from the VM side without knowing when native code is going to stop using
626 // them.
627 if (_jmethod_ids != nullptr) {
628 Method::clear_jmethod_ids(this);
629 }
630 }
885 void ClassLoaderData::free_deallocate_list() {
886 // This must be called at a safepoint because it depends on metadata walking at
887 // safepoint cleanup time.
888 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
889 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
890 if (_deallocate_list == nullptr) {
891 return;
892 }
893 // Go backwards because this removes entries that are freed.
894 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
895 Metadata* m = _deallocate_list->at(i);
896 if (!m->on_stack()) {
897 _deallocate_list->remove_at(i);
898 // There are only three types of metadata that we deallocate directly.
899 // Cast them so they can be used by the template function.
900 if (m->is_method()) {
901 MetadataFactory::free_metadata(this, (Method*)m);
902 } else if (m->is_constantPool()) {
903 MetadataFactory::free_metadata(this, (ConstantPool*)m);
904 } else if (m->is_klass()) {
905 if (!((Klass*)m)->is_inline_klass()) {
906 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
907 } else {
908 MetadataFactory::free_metadata(this, (InlineKlass*)m);
909 }
910 } else {
911 ShouldNotReachHere();
912 }
913 } else {
914 // Metadata is alive.
915 // If scratch_class is on stack then it shouldn't be on this list!
916 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
917 "scratch classes on this list should be dead");
918 // Also should assert that other metadata on the list was found in handles.
919 // Some cleaning remains.
920 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
921 }
922 }
923 }
924
925 // This is distinct from free_deallocate_list. For class loader data that are
926 // unloading, this frees the C heap memory for items on the list, and unlinks
927 // scratch or error classes so that unloading events aren't triggered for these
928 // classes. The metadata is removed with the unloading metaspace.
929 // There isn't C heap memory allocated for methods, so nothing is done for them.
|