--- old/src/hotspot/share/runtime/serviceThread.cpp 2019-07-11 14:50:02.444950781 -0400 +++ new/src/hotspot/share/runtime/serviceThread.cpp 2019-07-11 14:50:01.960932893 -0400 @@ -108,6 +108,7 @@ bool resolved_method_table_work = false; bool protection_domain_table_work = false; bool oopstorage_work = false; + bool deflate_idle_monitors = false; JvmtiDeferredEvent jvmti_event; { // Need state transition ThreadBlockInVM so that this thread @@ -133,10 +134,14 @@ (symboltable_work = SymbolTable::has_work()) | (resolved_method_table_work = ResolvedMethodTable::has_work()) | (protection_domain_table_work = SystemDictionary::pd_cache_table()->has_work()) | - (oopstorage_work = OopStorage::has_cleanup_work_and_reset())) - == 0) { + (oopstorage_work = OopStorage::has_cleanup_work_and_reset()) | + (deflate_idle_monitors = ObjectSynchronizer::is_async_deflation_needed()) + ) == 0) { // Wait until notified that there is some work to do. - ml.wait(); + // If AsyncDeflateIdleMonitors, then we wait for + // GuaranteedSafepointInterval so that is_async_deflation_needed() + // is checked at the same interval. + ml.wait(AsyncDeflateIdleMonitors ? GuaranteedSafepointInterval : 0); } if (has_jvmti_events) { @@ -179,6 +184,31 @@ if (oopstorage_work) { cleanup_oopstorages(oopstorages, oopstorage_count); } + + if (deflate_idle_monitors) { + // Deflate any global idle monitors. + ObjectSynchronizer::deflate_global_idle_monitors_using_JT(); + + // deflate_per_thread_idle_monitors_using_JT() is called by + // each JavaThread from ObjectSynchronizer::omAlloc() as needed. + int count = 0; + for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { + if (jt->omInUseCount > 0) { + // This JavaThread is using monitors so mark it. + jt->omShouldDeflateIdleMonitors = true; + count++; + } + } + if (count > 0) { + log_debug(monitorinflation)("requesting async deflation of idle monitors for %d thread(s).", count); + } + // The ServiceThread's async deflation request has been processed. + ObjectSynchronizer::set_is_async_deflation_requested(false); + + // The global in-use list was handled above, but the request won't + // be complete until the JavaThreads have handled their in-use + // lists. This is the nature of an async deflation request. + } } }