< prev index next >

src/hotspot/share/classfile/classLoaderData.cpp

Print this page

  53 #include "classfile/javaClasses.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/objArrayKlass.hpp"
  71 #include "oops/oop.inline.hpp"
  72 #include "oops/oopHandle.inline.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 = NULL;
  84 
  85 void ClassLoaderData::init_null_class_loader_data() {
  86   assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
  87   assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
  88 
  89   _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
  90   ClassLoaderDataGraph::_head = _the_null_class_loader_data;
  91   assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
  92 

 377 
 378 #ifdef ASSERT
 379     oop m = k->java_mirror();
 380     assert(m != NULL, "NULL mirror");
 381     assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
 382 #endif
 383     klass_closure->do_klass(k);
 384   }
 385 }
 386 
 387 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
 388   // Lock-free access requires load_acquire
 389   for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 390     if (k->is_instance_klass()) {
 391       f(InstanceKlass::cast(k));
 392     }
 393     assert(k != k->next_link(), "no loops!");
 394   }
 395 }
 396 










 397 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
 398   assert_locked_or_safepoint(Module_lock);
 399   if (_unnamed_module != NULL) {
 400     f(_unnamed_module);
 401   }
 402   if (_modules != NULL) {
 403     for (int i = 0; i < _modules->table_size(); i++) {
 404       for (ModuleEntry* entry = _modules->bucket(i);
 405            entry != NULL;
 406            entry = entry->next()) {
 407         f(entry);
 408       }
 409     }
 410   }
 411 }
 412 
 413 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
 414   assert_locked_or_safepoint(Module_lock);
 415   if (_packages != NULL) {
 416     for (int i = 0; i < _packages->table_size(); i++) {

 543   }
 544   ShouldNotReachHere();   // should have found this class!!
 545 }
 546 
 547 void ClassLoaderData::unload() {
 548   _unloading = true;
 549 
 550   LogTarget(Trace, class, loader, data) lt;
 551   if (lt.is_enabled()) {
 552     ResourceMark rm;
 553     LogStream ls(lt);
 554     ls.print("unload");
 555     print_value_on(&ls);
 556     ls.cr();
 557   }
 558 
 559   // Some items on the _deallocate_list need to free their C heap structures
 560   // if they are not already on the _klasses list.
 561   free_deallocate_list_C_heap_structures();
 562 


 563   // Clean up class dependencies and tell serviceability tools
 564   // these classes are unloading.  Must be called
 565   // after erroneous classes are released.
 566   classes_do(InstanceKlass::unload_class);
 567 
 568   // Method::clear_jmethod_ids only sets the jmethod_ids to NULL without
 569   // releasing the memory for related JNIMethodBlocks and JNIMethodBlockNodes.
 570   // This is done intentionally because native code (e.g. JVMTI agent) holding
 571   // jmethod_ids may access them after the associated classes and class loader
 572   // are unloaded. The Java Native Interface Specification says "method ID
 573   // does not prevent the VM from unloading the class from which the ID has
 574   // been derived. After the class is unloaded, the method or field ID becomes
 575   // invalid". In real world usages, the native code may rely on jmethod_ids
 576   // being NULL after class unloading. Hence, it is unsafe to free the memory
 577   // from the VM side without knowing when native code is going to stop using
 578   // them.
 579   if (_jmethod_ids != NULL) {
 580     Method::clear_jmethod_ids(this);
 581   }
 582 

 846 void ClassLoaderData::free_deallocate_list() {
 847   // This must be called at a safepoint because it depends on metadata walking at
 848   // safepoint cleanup time.
 849   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 850   assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
 851   if (_deallocate_list == NULL) {
 852     return;
 853   }
 854   // Go backwards because this removes entries that are freed.
 855   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 856     Metadata* m = _deallocate_list->at(i);
 857     if (!m->on_stack()) {
 858       _deallocate_list->remove_at(i);
 859       // There are only three types of metadata that we deallocate directly.
 860       // Cast them so they can be used by the template function.
 861       if (m->is_method()) {
 862         MetadataFactory::free_metadata(this, (Method*)m);
 863       } else if (m->is_constantPool()) {
 864         MetadataFactory::free_metadata(this, (ConstantPool*)m);
 865       } else if (m->is_klass()) {
 866         MetadataFactory::free_metadata(this, (InstanceKlass*)m);




 867       } else {
 868         ShouldNotReachHere();
 869       }
 870     } else {
 871       // Metadata is alive.
 872       // If scratch_class is on stack then it shouldn't be on this list!
 873       assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
 874              "scratch classes on this list should be dead");
 875       // Also should assert that other metadata on the list was found in handles.
 876       // Some cleaning remains.
 877       ClassLoaderDataGraph::set_should_clean_deallocate_lists();
 878     }
 879   }
 880 }
 881 
 882 // This is distinct from free_deallocate_list.  For class loader data that are
 883 // unloading, this frees the C heap memory for items on the list, and unlinks
 884 // scratch or error classes so that unloading events aren't triggered for these
 885 // classes. The metadata is removed with the unloading metaspace.
 886 // There isn't C heap memory allocated for methods, so nothing is done for them.

  53 #include "classfile/javaClasses.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/objArrayKlass.hpp"
  71 #include "oops/oop.inline.hpp"
  72 #include "oops/oopHandle.inline.hpp"
  73 #include "oops/inlineKlass.inline.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 = NULL;
  85 
  86 void ClassLoaderData::init_null_class_loader_data() {
  87   assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
  88   assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
  89 
  90   _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
  91   ClassLoaderDataGraph::_head = _the_null_class_loader_data;
  92   assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
  93 

 378 
 379 #ifdef ASSERT
 380     oop m = k->java_mirror();
 381     assert(m != NULL, "NULL mirror");
 382     assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
 383 #endif
 384     klass_closure->do_klass(k);
 385   }
 386 }
 387 
 388 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
 389   // Lock-free access requires load_acquire
 390   for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 391     if (k->is_instance_klass()) {
 392       f(InstanceKlass::cast(k));
 393     }
 394     assert(k != k->next_link(), "no loops!");
 395   }
 396 }
 397 
 398 void ClassLoaderData::inline_classes_do(void f(InlineKlass*)) {
 399   // Lock-free access requires load_acquire
 400   for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 401     if (k->is_inline_klass()) {
 402       f(InlineKlass::cast(k));
 403     }
 404     assert(k != k->next_link(), "no loops!");
 405   }
 406 }
 407 
 408 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
 409   assert_locked_or_safepoint(Module_lock);
 410   if (_unnamed_module != NULL) {
 411     f(_unnamed_module);
 412   }
 413   if (_modules != NULL) {
 414     for (int i = 0; i < _modules->table_size(); i++) {
 415       for (ModuleEntry* entry = _modules->bucket(i);
 416            entry != NULL;
 417            entry = entry->next()) {
 418         f(entry);
 419       }
 420     }
 421   }
 422 }
 423 
 424 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
 425   assert_locked_or_safepoint(Module_lock);
 426   if (_packages != NULL) {
 427     for (int i = 0; i < _packages->table_size(); i++) {

 554   }
 555   ShouldNotReachHere();   // should have found this class!!
 556 }
 557 
 558 void ClassLoaderData::unload() {
 559   _unloading = true;
 560 
 561   LogTarget(Trace, class, loader, data) lt;
 562   if (lt.is_enabled()) {
 563     ResourceMark rm;
 564     LogStream ls(lt);
 565     ls.print("unload");
 566     print_value_on(&ls);
 567     ls.cr();
 568   }
 569 
 570   // Some items on the _deallocate_list need to free their C heap structures
 571   // if they are not already on the _klasses list.
 572   free_deallocate_list_C_heap_structures();
 573 
 574   inline_classes_do(InlineKlass::cleanup);
 575 
 576   // Clean up class dependencies and tell serviceability tools
 577   // these classes are unloading.  Must be called
 578   // after erroneous classes are released.
 579   classes_do(InstanceKlass::unload_class);
 580 
 581   // Method::clear_jmethod_ids only sets the jmethod_ids to NULL without
 582   // releasing the memory for related JNIMethodBlocks and JNIMethodBlockNodes.
 583   // This is done intentionally because native code (e.g. JVMTI agent) holding
 584   // jmethod_ids may access them after the associated classes and class loader
 585   // are unloaded. The Java Native Interface Specification says "method ID
 586   // does not prevent the VM from unloading the class from which the ID has
 587   // been derived. After the class is unloaded, the method or field ID becomes
 588   // invalid". In real world usages, the native code may rely on jmethod_ids
 589   // being NULL after class unloading. Hence, it is unsafe to free the memory
 590   // from the VM side without knowing when native code is going to stop using
 591   // them.
 592   if (_jmethod_ids != NULL) {
 593     Method::clear_jmethod_ids(this);
 594   }
 595 

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