optimization of gc load, reduce gc in some scenarios am: 3dcd844ccc am: 8112f1ec46

Original change: https://android-review.googlesource.com/c/platform/art/+/1829433

Change-Id: I1b4ef08db5f83b801503c0d70bc5b28f0da52bce
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index c620dbc..70ee449 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -343,6 +343,7 @@
       old_native_bytes_allocated_(0),
       native_objects_notified_(0),
       num_bytes_freed_revoke_(0),
+      num_bytes_alive_after_gc_(0),
       verify_missing_card_marks_(false),
       verify_system_weaks_(false),
       verify_pre_gc_heap_(verify_pre_gc_heap),
@@ -2741,6 +2742,7 @@
   // Grow the heap so that we know when to perform the next GC.
   GrowForUtilization(collector, bytes_allocated_before_gc);
   old_native_bytes_allocated_.store(GetNativeBytes());
+  num_bytes_alive_after_gc_ = bytes_allocated_before_gc - current_gc_iteration_.GetFreedBytes();
   LogGC(gc_cause, collector);
   FinishGC(self, gc_type);
   // Actually enqueue all cleared references. Do this after the GC has officially finished since
@@ -3873,6 +3875,16 @@
     // For CC, we invoke a full compaction when going to the background, but the collector type
     // doesn't change.
     DCHECK_EQ(desired_collector_type_, kCollectorTypeCCBackground);
+    // App's allocations (since last GC) more than the threshold then do TransitionGC
+    // when the app was in background. If not then don't do TransitionGC.
+    size_t num_bytes_allocated_since_gc = GetBytesAllocated() - num_bytes_alive_after_gc_;
+    if (num_bytes_allocated_since_gc <
+        (UnsignedDifference(target_footprint_.load(std::memory_order_relaxed),
+                            num_bytes_alive_after_gc_)/4)
+        && !kStressCollectorTransition
+        && !IsLowMemoryMode()) {
+      return;
+    }
   }
   DCHECK_NE(collector_type_, kCollectorTypeCCBackground);
   CollectorTransitionTask* added_task = nullptr;
diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h
index 2c25d1d..1a95ea1 100644
--- a/runtime/gc/heap.h
+++ b/runtime/gc/heap.h
@@ -168,7 +168,7 @@
   // as object allocation time. time_to_call_mallinfo seems to be on the order of 1 usec
   // on Android.
 #ifdef __ANDROID__
-  static constexpr uint32_t kNotifyNativeInterval = 32;
+  static constexpr uint32_t kNotifyNativeInterval = 64;
 #else
   // Some host mallinfo() implementations are slow. And memory is less scarce.
   static constexpr uint32_t kNotifyNativeInterval = 384;
@@ -1475,6 +1475,10 @@
   // GC.
   Atomic<size_t> num_bytes_freed_revoke_;
 
+  // Records the number of bytes allocated at the time of GC, which is used later to calculate
+  // how many bytes have been allocated since the last GC
+  size_t num_bytes_alive_after_gc_;
+
   // Info related to the current or previous GC iteration.
   collector::Iteration current_gc_iteration_;