< prev index next >

src/hotspot/share/classfile/classLoaderData.cpp

Print this page

  51 #include "classfile/classLoaderDataGraph.inline.hpp"
  52 #include "classfile/dictionary.hpp"
  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/vmClasses.hpp"
  59 #include "logging/log.hpp"
  60 #include "logging/logStream.hpp"
  61 #include "memory/allocation.inline.hpp"
  62 #include "memory/classLoaderMetaspace.hpp"
  63 #include "memory/metadataFactory.hpp"
  64 #include "memory/metaspace.hpp"
  65 #include "memory/resourceArea.hpp"
  66 #include "memory/universe.hpp"
  67 #include "oops/access.inline.hpp"
  68 #include "oops/klass.inline.hpp"
  69 #include "oops/oop.inline.hpp"
  70 #include "oops/oopHandle.inline.hpp"

  71 #include "oops/weakHandle.inline.hpp"
  72 #include "runtime/arguments.hpp"
  73 #include "runtime/atomic.hpp"
  74 #include "runtime/handles.inline.hpp"
  75 #include "runtime/mutex.hpp"
  76 #include "runtime/safepoint.hpp"
  77 #include "utilities/growableArray.hpp"
  78 #include "utilities/macros.hpp"
  79 #include "utilities/ostream.hpp"
  80 
  81 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
  82 
  83 void ClassLoaderData::init_null_class_loader_data() {
  84   assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
  85   assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
  86 
  87   _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
  88   ClassLoaderDataGraph::_head = _the_null_class_loader_data;
  89   assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
  90 

 360 #ifdef ASSERT
 361       oop m = k->java_mirror();
 362       assert(m != NULL, "NULL mirror");
 363       assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
 364 #endif
 365       klass_closure->do_klass(k);
 366     }
 367   }
 368 }
 369 
 370 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
 371   // Lock-free access requires load_acquire
 372   for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 373     if (k->is_instance_klass()) {
 374       f(InstanceKlass::cast(k));
 375     }
 376     assert(k != k->next_link(), "no loops!");
 377   }
 378 }
 379 










 380 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
 381   assert_locked_or_safepoint(Module_lock);
 382   if (_unnamed_module != NULL) {
 383     f(_unnamed_module);
 384   }
 385   if (_modules != NULL) {
 386     for (int i = 0; i < _modules->table_size(); i++) {
 387       for (ModuleEntry* entry = _modules->bucket(i);
 388            entry != NULL;
 389            entry = entry->next()) {
 390         f(entry);
 391       }
 392     }
 393   }
 394 }
 395 
 396 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
 397   assert_locked_or_safepoint(Module_lock);
 398   if (_packages != NULL) {
 399     for (int i = 0; i < _packages->table_size(); i++) {

 526   }
 527   ShouldNotReachHere();   // should have found this class!!
 528 }
 529 
 530 void ClassLoaderData::unload() {
 531   _unloading = true;
 532 
 533   LogTarget(Trace, class, loader, data) lt;
 534   if (lt.is_enabled()) {
 535     ResourceMark rm;
 536     LogStream ls(lt);
 537     ls.print("unload");
 538     print_value_on(&ls);
 539     ls.cr();
 540   }
 541 
 542   // Some items on the _deallocate_list need to free their C heap structures
 543   // if they are not already on the _klasses list.
 544   free_deallocate_list_C_heap_structures();
 545 


 546   // Clean up class dependencies and tell serviceability tools
 547   // these classes are unloading.  Must be called
 548   // after erroneous classes are released.
 549   classes_do(InstanceKlass::unload_class);
 550 
 551   // Method::clear_jmethod_ids only sets the jmethod_ids to NULL without
 552   // releasing the memory for related JNIMethodBlocks and JNIMethodBlockNodes.
 553   // This is done intentionally because native code (e.g. JVMTI agent) holding
 554   // jmethod_ids may access them after the associated classes and class loader
 555   // are unloaded. The Java Native Interface Specification says "method ID
 556   // does not prevent the VM from unloading the class from which the ID has
 557   // been derived. After the class is unloaded, the method or field ID becomes
 558   // invalid". In real world usages, the native code may rely on jmethod_ids
 559   // being NULL after class unloading. Hence, it is unsafe to free the memory
 560   // from the VM side without knowing when native code is going to stop using
 561   // them.
 562   if (_jmethod_ids != NULL) {
 563     Method::clear_jmethod_ids(this);
 564   }
 565 

 828 void ClassLoaderData::free_deallocate_list() {
 829   // This must be called at a safepoint because it depends on metadata walking at
 830   // safepoint cleanup time.
 831   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 832   assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
 833   if (_deallocate_list == NULL) {
 834     return;
 835   }
 836   // Go backwards because this removes entries that are freed.
 837   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 838     Metadata* m = _deallocate_list->at(i);
 839     if (!m->on_stack()) {
 840       _deallocate_list->remove_at(i);
 841       // There are only three types of metadata that we deallocate directly.
 842       // Cast them so they can be used by the template function.
 843       if (m->is_method()) {
 844         MetadataFactory::free_metadata(this, (Method*)m);
 845       } else if (m->is_constantPool()) {
 846         MetadataFactory::free_metadata(this, (ConstantPool*)m);
 847       } else if (m->is_klass()) {
 848         MetadataFactory::free_metadata(this, (InstanceKlass*)m);




 849       } else {
 850         ShouldNotReachHere();
 851       }
 852     } else {
 853       // Metadata is alive.
 854       // If scratch_class is on stack then it shouldn't be on this list!
 855       assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
 856              "scratch classes on this list should be dead");
 857       // Also should assert that other metadata on the list was found in handles.
 858       // Some cleaning remains.
 859       ClassLoaderDataGraph::set_should_clean_deallocate_lists();
 860     }
 861   }
 862 }
 863 
 864 // This is distinct from free_deallocate_list.  For class loader data that are
 865 // unloading, this frees the C heap memory for items on the list, and unlinks
 866 // scratch or error classes so that unloading events aren't triggered for these
 867 // classes. The metadata is removed with the unloading metaspace.
 868 // There isn't C heap memory allocated for methods, so nothing is done for them.

  51 #include "classfile/classLoaderDataGraph.inline.hpp"
  52 #include "classfile/dictionary.hpp"
  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/vmClasses.hpp"
  59 #include "logging/log.hpp"
  60 #include "logging/logStream.hpp"
  61 #include "memory/allocation.inline.hpp"
  62 #include "memory/classLoaderMetaspace.hpp"
  63 #include "memory/metadataFactory.hpp"
  64 #include "memory/metaspace.hpp"
  65 #include "memory/resourceArea.hpp"
  66 #include "memory/universe.hpp"
  67 #include "oops/access.inline.hpp"
  68 #include "oops/klass.inline.hpp"
  69 #include "oops/oop.inline.hpp"
  70 #include "oops/oopHandle.inline.hpp"
  71 #include "oops/inlineKlass.inline.hpp"
  72 #include "oops/weakHandle.inline.hpp"
  73 #include "runtime/arguments.hpp"
  74 #include "runtime/atomic.hpp"
  75 #include "runtime/handles.inline.hpp"
  76 #include "runtime/mutex.hpp"
  77 #include "runtime/safepoint.hpp"
  78 #include "utilities/growableArray.hpp"
  79 #include "utilities/macros.hpp"
  80 #include "utilities/ostream.hpp"
  81 
  82 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
  83 
  84 void ClassLoaderData::init_null_class_loader_data() {
  85   assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
  86   assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
  87 
  88   _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
  89   ClassLoaderDataGraph::_head = _the_null_class_loader_data;
  90   assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
  91 

 361 #ifdef ASSERT
 362       oop m = k->java_mirror();
 363       assert(m != NULL, "NULL mirror");
 364       assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
 365 #endif
 366       klass_closure->do_klass(k);
 367     }
 368   }
 369 }
 370 
 371 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
 372   // Lock-free access requires load_acquire
 373   for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 374     if (k->is_instance_klass()) {
 375       f(InstanceKlass::cast(k));
 376     }
 377     assert(k != k->next_link(), "no loops!");
 378   }
 379 }
 380 
 381 void ClassLoaderData::inline_classes_do(void f(InlineKlass*)) {
 382   // Lock-free access requires load_acquire
 383   for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
 384     if (k->is_inline_klass()) {
 385       f(InlineKlass::cast(k));
 386     }
 387     assert(k != k->next_link(), "no loops!");
 388   }
 389 }
 390 
 391 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
 392   assert_locked_or_safepoint(Module_lock);
 393   if (_unnamed_module != NULL) {
 394     f(_unnamed_module);
 395   }
 396   if (_modules != NULL) {
 397     for (int i = 0; i < _modules->table_size(); i++) {
 398       for (ModuleEntry* entry = _modules->bucket(i);
 399            entry != NULL;
 400            entry = entry->next()) {
 401         f(entry);
 402       }
 403     }
 404   }
 405 }
 406 
 407 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
 408   assert_locked_or_safepoint(Module_lock);
 409   if (_packages != NULL) {
 410     for (int i = 0; i < _packages->table_size(); i++) {

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

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