--- old/src/hotspot/share/memory/metaspace.cpp 2018-11-01 10:58:11.692875793 -0700 +++ new/src/hotspot/share/memory/metaspace.cpp 2018-11-01 10:58:11.308861274 -0700 @@ -1277,13 +1277,18 @@ 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. - result = Universe::heap()->satisfy_failed_metadata_allocation(loader_data, word_size, mdtype); + if (DumpSharedSpaces && THREAD->is_VM_thread()) { + // We cannot GC if we are at the end of the CDS dumping stage which runs inside + // the VM thread, so just expand. We won't be allocating much anyway. + result = loader_data->metaspace_non_null()->expand_and_allocate(word_size, mdtype); + } else { + // 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); + } } }