--- old/src/share/vm/services/memTracker.cpp Fri Jul 13 08:04:54 2012 +++ new/src/share/vm/services/memTracker.cpp Fri Jul 13 08:04:52 2012 @@ -361,6 +361,10 @@ // VM state, so it can stop at safepoint. JavaThread running in VM state does not // need lock to write records. if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) { + assert(!(SafepointSynchronize::is_at_safepoint() && + ((JavaThread*)thread)->thread_state() == _thread_blocked), + "_thread_blocked allocate/deallocation memory at safepoint"); + if (((JavaThread*)thread)->thread_state() == _thread_in_native) { ThreadInVMfromNative trans((JavaThread*)thread); create_record_in_recorder(addr, flags, size, pc, thread); @@ -486,8 +490,6 @@ // now, it is the time to shut whole things off if (_state == NMT_final_shutdown) { - _tracking_level = NMT_off; - // walk all JavaThreads to delete all recorders SyncThreadRecorderClosure stc; Threads::threads_do(&stc); @@ -499,8 +501,16 @@ _global_recorder = NULL; } } - - _state = NMT_shutdown; + MemRecorder* pending_recorders = get_pending_recorders(); + if (pending_recorders != NULL) { + delete pending_recorders; + } + // wait until MemRecorder instance drops to zero to + // completely shutdown NMT + if (MemRecorder::_instance_count == 0) { + _state = NMT_shutdown; + _tracking_level = NMT_off; + } } }