src/share/vm/gc/g1/g1CollectedHeap.cpp

Print this page

        

@@ -1726,10 +1726,42 @@
                   maximum_desired_capacity, (double) MaxHeapFreeRatio);
     shrink(shrink_bytes);
   }
 }
 
+HeapWord*
+G1CollectedHeap::satisfy_failed_allocation_helper(size_t word_size,
+                                           AllocationContext_t context,
+                                           bool clear_all_soft_refs,
+                                           bool* succeeded) {
+  assert_at_safepoint(true /* should_be_vm_thread */);
+
+  *succeeded = true;
+  bool gc_succeeded = do_collection(false, /* explicit_gc */
+                                    clear_all_soft_refs,
+                                    word_size);
+  if (!gc_succeeded) {
+    *succeeded = false;
+    return NULL;
+  }
+
+  // Retry the allocation
+  HeapWord* result = attempt_allocation_at_safepoint(word_size,
+                                           context,
+                                           true /* expect_null_mutator_alloc_region */);
+  if (result != NULL) {
+    assert(*succeeded, "sanity");
+    return result;
+  }
+
+  // We may shrink heap too much after Full GC.
+  // Need to expand heap.
+  result = expand_and_allocate(word_size, context);
+
+  assert(*succeeded, "sanity");
+  return result;
+}
 
 HeapWord*
 G1CollectedHeap::satisfy_failed_allocation(size_t word_size,
                                            AllocationContext_t context,
                                            bool* succeeded) {

@@ -1755,42 +1787,24 @@
     assert(*succeeded, "sanity");
     return result;
   }
 
   // Expansion didn't work, we'll try to do a Full GC.
-  bool gc_succeeded = do_collection(false, /* explicit_gc */
-                                    false, /* clear_all_soft_refs */
-                                    word_size);
-  if (!gc_succeeded) {
-    *succeeded = false;
-    return NULL;
-  }
-
-  // Retry the allocation
-  result = attempt_allocation_at_safepoint(word_size,
+  result = satisfy_failed_allocation_helper(word_size,
                                            context,
-                                           true /* expect_null_mutator_alloc_region */);
-  if (result != NULL) {
-    assert(*succeeded, "sanity");
+                                            false, /* clear_all_soft_refs */
+                                            succeeded);
+  if (!*succeeded || result != NULL) {
     return result;
   }
 
   // Then, try a Full GC that will collect all soft references.
-  gc_succeeded = do_collection(false, /* explicit_gc */
-                               true,  /* clear_all_soft_refs */
-                               word_size);
-  if (!gc_succeeded) {
-    *succeeded = false;
-    return NULL;
-  }
-
-  // Retry the allocation once more
-  result = attempt_allocation_at_safepoint(word_size,
+  result = satisfy_failed_allocation_helper(word_size,
                                            context,
-                                           true /* expect_null_mutator_alloc_region */);
-  if (result != NULL) {
-    assert(*succeeded, "sanity");
+                                            true, /* clear_all_soft_refs */
+                                            succeeded);
+  if (!*succeeded || result != NULL) {
     return result;
   }
 
   assert(!collector_policy()->should_clear_all_soft_refs(),
          "Flag should have been handled and cleared prior to this point");