diff -r 1256c8d923e8 -r e0fd97beab7e src/hotspot/share/memory/metaspace.cpp --- a/src/hotspot/share/memory/metaspace.cpp Fri Nov 09 12:08:53 2018 +0800 +++ b/src/hotspot/share/memory/metaspace.cpp Thu Nov 01 10:59:05 2018 -0700 @@ -1260,6 +1260,8 @@ MetaWord* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, TRAPS) { assert(!_frozen, "sanity"); + assert(!(DumpSharedSpaces && THREAD->is_VM_thread()), "sanity"); + if (HAS_PENDING_EXCEPTION) { assert(false, "Should not allocate with exception pending"); return NULL; // caller does a CHECK_NULL too @@ -1277,12 +1279,10 @@ tracer()->report_metaspace_allocation_failure(loader_data, word_size, type, mdtype); // Allocation failed. - if (is_init_completed() && !(DumpSharedSpaces && THREAD->is_VM_thread())) { + if (is_init_completed()) { // Only start a GC if the bootstrapping has completed. - // Also, we cannot GC if we are at the end of the CDS dumping stage which runs inside - // the VM thread. - - // Try to clean out some memory and retry. + // Try to clean out some heap memory and retry. This can prevent premature + // expansion of the metaspace. result = Universe::heap()->satisfy_failed_metadata_allocation(loader_data, word_size, mdtype); } } @@ -1408,6 +1408,7 @@ } ClassLoaderMetaspace::~ClassLoaderMetaspace() { + Metaspace::assert_not_frozen(); DEBUG_ONLY(Atomic::inc(&g_internal_statistics.num_metaspace_deaths)); delete _vsm; if (Metaspace::using_class_space()) {