From f72e3aeb10e41e0a7bcc77acea549cda44703b1d Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Wed, 24 Sep 2025 17:35:10 -0700 Subject: [PATCH] [scudo] Release to OS if a large amount of memory is deallocated. Before this change, if large amounts of memory are deallocated within a release interval, the release is put off until the release interval occurs. Unfortunately, for larger class sizes, this could mean that a lot of this memory accumulates and is never released since no more deallocations occur in that size class. To fix this, if `RegionPushedBytesDelta` grows larger than a group size, immediately do a release. This work was originally done by ChiaHungDuan. --- compiler-rt/lib/scudo/standalone/primary64.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h index d08103008ef7c..747b1a2233d32 100644 --- a/compiler-rt/lib/scudo/standalone/primary64.h +++ b/compiler-rt/lib/scudo/standalone/primary64.h @@ -1565,6 +1565,13 @@ bool SizeClassAllocator64::hasChanceToReleasePages( if (DiffSinceLastReleaseNs < 2 * IntervalNs) return false; } else if (DiffSinceLastReleaseNs < IntervalNs) { + // `TryReleaseThreshold` is capped by (1UL << GroupSizeLog) / 2). If + // RegionPushedBytesDelta grows to twice the threshold, it implies some + // huge deallocations have happened so we better try to release some + // pages. Note this tends to happen for larger block sizes. + if (RegionPushedBytesDelta > (1ULL << GroupSizeLog)) + return true; + // In this case, we are over the threshold but we just did some page // release in the same release interval. This is a hint that we may want // a higher threshold so that we can release more memory at once.