mirror of
https://github.com/oven-sh/bun
synced 2026-02-13 20:39:05 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user