diff --git a/rts/motoko-rts/src/gc/incremental.rs b/rts/motoko-rts/src/gc/incremental.rs index b136ddea147..1ebb257b310 100644 --- a/rts/motoko-rts/src/gc/incremental.rs +++ b/rts/motoko-rts/src/gc/incremental.rs @@ -72,22 +72,32 @@ unsafe fn should_start() -> bool { use self::partitioned_heap::PARTITION_SIZE; use crate::memory::ic::partitioned_memory; - const CRITICAL_HEAP_LIMIT: Bytes = Bytes(u32::MAX - 768 * 1024 * 1024); - const CRITICAL_GROWTH_THRESHOLD: f64 = 0.01; - const NORMAL_GROWTH_THRESHOLD: f64 = 0.65; - let heap_size = partitioned_memory::get_heap_size(); - let growth_threshold = if heap_size > CRITICAL_HEAP_LIMIT { - CRITICAL_GROWTH_THRESHOLD - } else { - NORMAL_GROWTH_THRESHOLD - }; + if heap_size.as_usize() < PARTITION_SIZE { + return false; + } + + const ABSOLUTE_GROWTH_THRESHOLD: Bytes = Bytes(512 * 1024 * 1024); let current_allocations = partitioned_memory::get_total_allocations(); debug_assert!(current_allocations >= LAST_ALLOCATIONS); let absolute_growth = current_allocations - LAST_ALLOCATIONS; + if absolute_growth > ABSOLUTE_GROWTH_THRESHOLD { + return true; + } + + const CRITICAL_HEAP_LIMIT: Bytes = Bytes(u32::MAX - 768 * 1024 * 1024); + const CRITICAL_RELATIVE_GROWTH_THRESHOLD: f64 = 0.01; + const NORMAL_RELATIVE_GROWTH_THRESHOLD: f64 = 0.65; + + let relative_growth_threshold = if heap_size > CRITICAL_HEAP_LIMIT { + CRITICAL_RELATIVE_GROWTH_THRESHOLD + } else { + NORMAL_RELATIVE_GROWTH_THRESHOLD + }; + let relative_growth = absolute_growth.0 as f64 / heap_size.as_usize() as f64; - relative_growth > growth_threshold && heap_size.as_usize() >= PARTITION_SIZE + relative_growth > relative_growth_threshold } #[cfg(feature = "ic")]