Shrink memory in threadpool (#21689)

### What does this PR do?

After 10s of inactivity in the thread pool, this releases memory more
aggressively back to the operating system

### How did you verify your code works?
This commit is contained in:
Jarred Sumner
2025-08-07 22:33:12 -07:00
committed by GitHub
parent 92f896ddd7
commit 428c8d4bbf
3 changed files with 19 additions and 1 deletions

View File

@@ -660,6 +660,7 @@ const Event = struct {
noinline fn wait(self: *Event) void {
var acquire_with: u32 = EMPTY;
var state = self.state.load(.monotonic);
var has_shrunk_memory: bool = false;
while (true) {
// If we're shutdown then exit early.
@@ -699,7 +700,11 @@ const Event = struct {
// Acquiring to WAITING will make the next notify() or shutdown() wake a sleeping futex thread
// who will either exit on SHUTDOWN or acquire with WAITING again, ensuring all threads are awoken.
// This unfortunately results in the last notify() or shutdown() doing an extra futex wake but that's fine.
Futex.wait(&self.state, WAITING, null) catch unreachable;
Futex.wait(&self.state, WAITING, if (!has_shrunk_memory) std.time.ns_per_s * 10 else null) catch {
has_shrunk_memory = true;
bun.Global.mimalloc_cleanup(false);
bun.jsc.wtf.releaseFastMallocFreeMemoryForThisThread();
};
state = self.state.load(.monotonic);
acquire_with = WAITING;
}