Skip to content

Commit b49effd

Browse files
zengkaifakdrag0n
authored andcommitted
optimization of gc load, reduce gc in some scenarios
1.NativeAlloc GC,Increase the count of the number of small native memory allocations. 2.CollectTransition GC,App's allocations (since last GC) more than the threshold then do GC when the app was in background. If not then don't do GC. Expert in charge of ART, please evaluate this CL and hope to merge it in master branch. Bug:200116730 Change-Id: Id2977f05eb249691326955e6f2424d4e5e08b417
1 parent cac15bc commit b49effd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

runtime/gc/heap.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ Heap::Heap(size_t initial_size,
333333
old_native_bytes_allocated_(0),
334334
native_objects_notified_(0),
335335
num_bytes_freed_revoke_(0),
336+
num_bytes_alive_after_gc_(0),
336337
verify_missing_card_marks_(false),
337338
verify_system_weaks_(false),
338339
verify_pre_gc_heap_(verify_pre_gc_heap),
@@ -2719,6 +2720,7 @@ collector::GcType Heap::CollectGarbageInternal(collector::GcType gc_type,
27192720
// Grow the heap so that we know when to perform the next GC.
27202721
GrowForUtilization(collector, bytes_allocated_before_gc);
27212722
old_native_bytes_allocated_.store(GetNativeBytes());
2723+
num_bytes_alive_after_gc_ = bytes_allocated_before_gc - current_gc_iteration_.GetFreedBytes();
27222724
LogGC(gc_cause, collector);
27232725
FinishGC(self, gc_type);
27242726
// Actually enqueue all cleared references. Do this after the GC has officially finished since
@@ -3840,6 +3842,16 @@ void Heap::RequestCollectorTransition(CollectorType desired_collector_type, uint
38403842
// For CC, we invoke a full compaction when going to the background, but the collector type
38413843
// doesn't change.
38423844
DCHECK_EQ(desired_collector_type_, kCollectorTypeCCBackground);
3845+
// App's allocations (since last GC) more than the threshold then do TransitionGC
3846+
// when the app was in background. If not then don't do TransitionGC.
3847+
size_t num_bytes_allocated_since_gc = GetBytesAllocated() - num_bytes_alive_after_gc_;
3848+
if (num_bytes_allocated_since_gc <
3849+
(UnsignedDifference(target_footprint_.load(std::memory_order_relaxed),
3850+
num_bytes_alive_after_gc_)/4)
3851+
&& !kStressCollectorTransition
3852+
&& !IsLowMemoryMode()) {
3853+
return;
3854+
}
38433855
}
38443856
DCHECK_NE(collector_type_, kCollectorTypeCCBackground);
38453857
CollectorTransitionTask* added_task = nullptr;

runtime/gc/heap.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Heap {
166166
// as object allocation time. time_to_call_mallinfo seems to be on the order of 1 usec
167167
// on Android.
168168
#ifdef __ANDROID__
169-
static constexpr uint32_t kNotifyNativeInterval = 32;
169+
static constexpr uint32_t kNotifyNativeInterval = 64;
170170
#else
171171
// Some host mallinfo() implementations are slow. And memory is less scarce.
172172
static constexpr uint32_t kNotifyNativeInterval = 384;
@@ -1468,6 +1468,10 @@ class Heap {
14681468
// GC.
14691469
Atomic<size_t> num_bytes_freed_revoke_;
14701470

1471+
// Records the number of bytes allocated at the time of GC, which is used later to calculate
1472+
// how many bytes have been allocated since the last GC
1473+
size_t num_bytes_alive_after_gc_;
1474+
14711475
// Info related to the current or previous GC iteration.
14721476
collector::Iteration current_gc_iteration_;
14731477

0 commit comments

Comments
 (0)