From df18fd7ab6fa6955a51f5caf6d1e0e7246739a6a Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 1 Jul 2025 00:17:35 -0400 Subject: [PATCH] libroot/malloc: Add a free maximum to PagesAllocator. 128 MB should be more than enough memory in the global cache for most applications (this limit's hit once the application has 512 MB of live allocations, based on the current free percentage of 25%.) --- src/system/libroot/posix/malloc/openbsd/PagesAllocator.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/system/libroot/posix/malloc/openbsd/PagesAllocator.cpp b/src/system/libroot/posix/malloc/openbsd/PagesAllocator.cpp index 60f06e5c22..816e133f2c 100644 --- a/src/system/libroot/posix/malloc/openbsd/PagesAllocator.cpp +++ b/src/system/libroot/posix/malloc/openbsd/PagesAllocator.cpp @@ -32,6 +32,9 @@ static const size_t kReserveAddressSpace = 128 * 1024 * 1024; /*! Cache up to this many percentage points of free memory (compared to used.) */ static const size_t kFreePercentage = 25; +/*! Always forbid more free memory than this, even if it's below kFreePercentage. */ +static const size_t kFreeMaximum = 128 * 1024 * 1024; + /*! Always allow this much free memory, even if it's above kFreePercentage. */ static const size_t kFreeMinimum = 128 * kPageSize; @@ -252,6 +255,7 @@ public: } chunk = previousChunk; } + status_t status = _UnlockingRemoveAndUnmap(chunk); if (status != B_OK) return status; @@ -320,6 +324,8 @@ private: size_t freeLimit = ((fUsed * kFreePercentage) / 100); if (freeLimit < kFreeMinimum) freeLimit = kFreeMinimum; + else if (freeLimit > kFreeMaximum) + freeLimit = kFreeMaximum; else freeLimit = (freeLimit + (kPageSize - 1)) & ~(kPageSize - 1); return freeLimit;