diff -r 2e91d927e00c src/hotspot/share/runtime/thread.cpp
--- a/src/hotspot/share/runtime/thread.cpp	Sat Aug 18 13:59:25 2018 -0400
+++ b/src/hotspot/share/runtime/thread.cpp	Sun Aug 19 20:55:26 2018 -0400
@@ -1964,6 +1964,7 @@
     _timer_exit_phase3.stop();
     _timer_exit_phase4.start();
   }
+  release_safepoint_aware_resources();
   // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread
   Threads::remove(this);
 
@@ -2005,6 +2006,7 @@
 
   BarrierSet::barrier_set()->on_thread_detach(this);
 
+  release_safepoint_aware_resources();
   Threads::remove(this);
   this->smr_delete();
 }
@@ -3297,13 +3299,16 @@
 }
 
 CompilerThread::~CompilerThread() {
+  // Delete objects which were allocated on heap.
+  delete _counters;
+}
+
+void CompilerThread::release_safepoint_aware_resources() {
   // Free buffer blob, if allocated
   if (get_buffer_blob() != NULL) {
     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
     CodeCache::free(get_buffer_blob());
   }
-  // Delete objects which were allocated on heap.
-  delete _counters;
 }
 
 bool CompilerThread::can_call_java() const {
diff -r 2e91d927e00c src/hotspot/share/runtime/thread.hpp
--- a/src/hotspot/share/runtime/thread.hpp	Sat Aug 18 13:59:25 2018 -0400
+++ b/src/hotspot/share/runtime/thread.hpp	Sun Aug 19 20:55:26 2018 -0400
@@ -1209,6 +1209,15 @@
   inline void set_polling_page(void* poll_value);
   inline volatile void* get_polling_page();
 
+ protected:
+  // Release resources that participate safepoints, but are not private to this thread.
+  // E.g. resources that are still visible to GC scanning during safepoints, even
+  // this thread no longer participates.
+  // After this call, this thread will be removed from Threads list, therefore no longer
+  // participates in safepointing, any manipulations to resources that are still visible to
+  // safepoint scans after this call, will be racy and dangerous.
+  virtual void release_safepoint_aware_resources() { }
+
  private:
   // Support for thread handshake operations
   HandshakeState _handshake;
@@ -2088,6 +2097,9 @@
   // Get/set the thread's current task
   CompileTask* task()                      { return _task; }
   void         set_task(CompileTask* task) { _task = task; }
+
+ protected:
+  virtual void release_safepoint_aware_resources();
 };
 
 inline CompilerThread* CompilerThread::current() {
