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 }
868 void ClassLoaderData::free_deallocate_list() {
869 // This must be called at a safepoint because it depends on metadata walking at
870 // safepoint cleanup time.
871 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
872 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
873 if (_deallocate_list == nullptr) {
874 return;
875 }
876 // Go backwards because this removes entries that are freed.
877 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
878 Metadata* m = _deallocate_list->at(i);
879 if (!m->on_stack()) {
880 _deallocate_list->remove_at(i);
881 // There are only three types of metadata that we deallocate directly.
882 // Cast them so they can be used by the template function.
883 if (m->is_method()) {
884 MetadataFactory::free_metadata(this, (Method*)m);
885 } else if (m->is_constantPool()) {
886 MetadataFactory::free_metadata(this, (ConstantPool*)m);
887 } else if (m->is_klass()) {
888 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
889 } else {
890 ShouldNotReachHere();
891 }
892 } else {
893 // Metadata is alive.
894 // If scratch_class is on stack then it shouldn't be on this list!
895 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
896 "scratch classes on this list should be dead");
897 // Also should assert that other metadata on the list was found in handles.
898 // Some cleaning remains.
899 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
900 }
901 }
902 }
903
904 // This is distinct from free_deallocate_list. For class loader data that are
905 // unloading, this frees the C heap memory for items on the list, and unlinks
906 // scratch or error classes so that unloading events aren't triggered for these
907 // classes. The metadata is removed with the unloading metaspace.
908 // 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 }
881 void ClassLoaderData::free_deallocate_list() {
882 // This must be called at a safepoint because it depends on metadata walking at
883 // safepoint cleanup time.
884 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
885 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
886 if (_deallocate_list == nullptr) {
887 return;
888 }
889 // Go backwards because this removes entries that are freed.
890 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
891 Metadata* m = _deallocate_list->at(i);
892 if (!m->on_stack()) {
893 _deallocate_list->remove_at(i);
894 // There are only three types of metadata that we deallocate directly.
895 // Cast them so they can be used by the template function.
896 if (m->is_method()) {
897 MetadataFactory::free_metadata(this, (Method*)m);
898 } else if (m->is_constantPool()) {
899 MetadataFactory::free_metadata(this, (ConstantPool*)m);
900 } else if (m->is_klass()) {
901 if (!((Klass*)m)->is_inline_klass()) {
902 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
903 } else {
904 MetadataFactory::free_metadata(this, (InlineKlass*)m);
905 }
906 } else {
907 ShouldNotReachHere();
908 }
909 } else {
910 // Metadata is alive.
911 // If scratch_class is on stack then it shouldn't be on this list!
912 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
913 "scratch classes on this list should be dead");
914 // Also should assert that other metadata on the list was found in handles.
915 // Some cleaning remains.
916 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
917 }
918 }
919 }
920
921 // This is distinct from free_deallocate_list. For class loader data that are
922 // unloading, this frees the C heap memory for items on the list, and unlinks
923 // scratch or error classes so that unloading events aren't triggered for these
924 // classes. The metadata is removed with the unloading metaspace.
925 // There isn't C heap memory allocated for methods, so nothing is done for them.
|