Optimize GC judgment process from foreground to background
when app goes from the foreground to the background,GC
will occur after 5s when memory grows more than a quarter
of what it can grow. But if the memory of the process
changes during these 5s, such as triggering GC, then
the above judgment conditions will be meaningless.
It would be more reasonable to judge the memory growth
at the time of execution of the task, And can avoid
repeated GC.
Bug: 243053465
Signed-off-by: qiubowen <qiubowen@xiaomi.com>
Change-Id: I89796ab24194c2a8cdff9e8ed8e3913a51bfdc62
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index a8195a3..dbe9161 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -1525,6 +1525,20 @@
void Heap::DoPendingCollectorTransition() {
CollectorType desired_collector_type = desired_collector_type_;
+
+ if (collector_type_ == kCollectorTypeCC) {
+ // 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;
+ }
+ }
+
// Launch homogeneous space compaction if it is desired.
if (desired_collector_type == kCollectorTypeHomogeneousSpaceCompact) {
if (!CareAboutPauseTimes()) {
@@ -1538,7 +1552,7 @@
// Invoke CC full compaction.
CollectGarbageInternal(collector::kGcTypeFull,
kGcCauseCollectorTransition,
- /*clear_soft_references=*/false, GC_NUM_ANY);
+ /*clear_soft_references=*/false, GetCurrentGcNum() + 1);
} else {
VLOG(gc) << "CC background compaction ignored due to jank perceptible process state";
}
@@ -3927,16 +3941,6 @@
// 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;