src/share/vm/services/gcNotifier.cpp

Print this page

        

@@ -98,13 +98,20 @@
 static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) {
 
   // Fill the arrays of MemoryUsage objects with before and after GC
   // per pool memory usage
 
-  klassOop muKlass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);   objArrayOop bu = oopFactory::new_objArray( muKlass,MemoryService::num_memory_pools(), CHECK_NH);
+  klassOop mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);
+  instanceKlassHandle mu_kh(THREAD, mu_klass);
+
+  // The array allocations should use a handle containing mu_klass
+  // as the first allocation could trigger a GC, causing the actual
+  // klass oop to move, and leaving mu_klass pointing to the old
+  // location.
+  objArrayOop bu = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);
   objArrayHandle usage_before_gc_ah(THREAD, bu);
-  objArrayOop au = oopFactory::new_objArray(muKlass,MemoryService::num_memory_pools(), CHECK_NH);
+  objArrayOop au = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);
   objArrayHandle usage_after_gc_ah(THREAD, au);
 
   for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
     Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH);
     Handle after_usage;