Repository navigation
page: a few MB of recently used large pages are left alone for a while (no timer) - #50
Merged
Merged
Conversation
…lone for a while Since the idle sweep discards the free blocks of large pages (#46), a thread that handles a request now and then takes every buffer of 96 KiB and up out of discarded memory again: once the two epochs of the hold are over the blocks go, and a server with five requests a second pays a page fault for every OS page of every buffer on every request. purge_holes_large_floor (4 MiB) bytes of such blocks, process-wide, are now left where they are while their page was allocated from in the last purge_holes_large_floor_epochs (256) epochs of the sweep. - The budget is one atomic sum. A thread gives its share back when its next sweep decides again, when it exits, and in the child of a fork; what it kept and has used since counts until then (stale on the high side only). - The pages of one sweep are decided together at the end of its pass over the thread's own pages, the ones that were used last first. Abandoned pages come after that for what is left; one that stays says in swept_state which thread counts it, so that the other threads that sweep its heap leave it alone and do not count it again (until a thread that had a share ends: then the first sweeper to pass counts it). - Only blocks on the free list count: the part of a page that was never formed was never written to, and it is discarded as before. - The age of a page is the epoch of its last allocation, which is in swept_state already. An epoch ends only when a thread is swept, so there is no clock in the page and nothing is woken for this: a process in which no thread parks any more keeps those bytes until one does. There is no floor without an interval to count epochs in. - A thread that stays parked is looked at again by the scavenger once what it kept is old enough to go, which is when the sweeps of other threads have moved the epoch that far: it does not hold the floor against the threads that run. The idea of a floor under what the sweeps take is from #48 (purge_holes_large_floor, there a flat number of bytes that stay for good). This one counts what was written to, prefers what was used last, and lets go of it.
Most of the page faults of such a thread are not for free blocks in a page that is still in use: the buffers of a request come back to a page of their own once the collector has freed them, that page is freed when its last block is (or the sweep finds it with all its blocks free and frees it), and the next request makes and faults in a new one: four new large pages for every ten requests of 256 KiB in a server. Such a page is now held as its free blocks would be: for the two epochs of the hold, and then under purge_holes_large_floor while it was allocated from in the last purge_holes_large_floor_epochs epochs. Until a sweep gets to decide, for a thread that is swept at all: where its last block is freed the page is retired like a small one instead of freed (so a thread that does not park any more holds it for MI_RETIRE_CYCLES and no longer), and the collect that an idle thread does right before it is swept leaves it to that sweep. With a floor of 0 such a page is freed where it is found, as before.
test-purge-holes-large: floor (what stays is bounded, comes out the same at the next sweep, is counted once when another thread sweeps the same heap, and a thread that ends gives its share back), floor-decay (the pages that were used last take the floor from the ones before, time alone takes nothing, nothing stays once the pages were not allocated from for that many epochs), floor-free-page (a page with no block in use stays, is taken by the next allocation, and goes; no floor without an interval). The floor of a case is derived from what it frees, whatever a large page and its blocks are on the platform. test-park-handoff: a thread that stays parked keeps what is under the floor and nobody is woken for it; it goes once the parks of another thread have moved the epoch far enough, though the thread never left its park.
…ts them when it ends Not only one that has a share of the floor at that moment: a sweep that its owner cut short after the share was given back has not counted its abandoned pages again, and those still name it.
…e path of every other page _mi_page_retire asks it where it would free the page, in a function of its own: the allocator micro benchmarks (random malloc/free of small blocks, malloc+free of one 128 KiB block) take the same number of instructions as before to the last digit; with the question inline the second took 5% more.
Jarred-Sumner
added a commit
to oven-sh/bun
that referenced
this pull request
Sep 15, 2026
, #51): recently used large pages are left alone for a while (#42808) ## What `MIMALLOC_COMMIT` 69050cf3 → 6df7aa13, the head of `bun-dev3-v2`. Relative to main's pin that is three fork PRs: - **oven-sh/mimalloc#50**, what this PR is about: see below. - **oven-sh/mimalloc#49**: the pthread key of the allocator's thread-local slots is created once when several threads first use a second heap at the same time (macOS; the code is not compiled on Linux). Measured there: every hot path is the same instruction for instruction. - **oven-sh/mimalloc#47**: a profiler that is started again starts every thread over. Only reachable through the sampling-profiler hooks, which nothing on main calls yet; #42626 (`Bun.pprof.heap`) bumps the pin to that commit for its own sake. Whichever of the two PRs lands second keeps the newer commit, which is this one. Since #42668 the idle sweep hands back the free blocks of the allocator's 4 MiB pages (buffers of 96 KiB and up) two sweeps after the page was last allocated from, which is 100–200 ms. A server sits idle between two requests for longer than that, so it took the buffers of every request out of discarded memory again: a page fault for each 4 KiB of them, on every request. About half of that was free blocks in a page that still had a buffer in use; the other half was pages with no buffer in use at all, which were freed outright and made anew for the next request. oven-sh/mimalloc#50: a large page (its free blocks, or all of it) is left alone until it has not been allocated from for 256 epochs of the sweep, for up to 4 MiB process-wide, the pages that were used last first. There is no timer in this: an epoch ends only when some thread is swept. `MIMALLOC_PURGE_HOLES_LARGE_FLOOR=0` is the behaviour before (a bare number there is bytes: write `4MiB`, not `4096`). ## What it costs - A process in which no thread ever parks again keeps up to 4 MiB of large pages that it would have given back. Measured with the allocator alone: 24 blocks of 300 KiB, 20 freed, a few short parks and then one for good: 4460 KiB anonymous at every reading for 70 s against 1460 KiB before (3.0 MB of the 6 MB freed; a page stays or goes whole). The allocator's scavenger thread wakes for its 30 s safety timeout as before and for nothing else. - An idle server holds 0.2–1.0 MB more ten seconds after the load (3 MB after 512 KiB bodies) and the same as before a minute after. - A large application parked at a prompt: 1 MB. ## Numbers Linux x64, release builds of this tree, the mimalloc commit the only difference. `Bun.serve` that answers a request with its own body, one connection, 5 requests a second for 20 s. Minor faults of the server (`/proc/<pid>/stat`) and kernel instructions (`perf stat -e instructions:k -p`), per request: | body | main: faults | kernel instr | this PR: faults | kernel instr | |---|---|---|---|---| | 128 KiB | 65.4 | 627 K | 2.6 | 208 K | | 256 KiB | 123.9 | 1031 K | 2.5 | 375 K | | 512 KiB | 309.1 | 2331 K | 14.1 | 898 K | | `hello` | 1.1 | 128 K | 1.1 | 114 K | (No latencies: the machine was busy with other work and the median of one binary moved more from run to run than the two differ. At 512 KiB it shows through: 2.60 ms → 2.08 ms.) Idle afterwards, anonymous RSS of the server in KiB at +10 s / +70 s: 128 KiB 3128 / 2908 → 4044 / 2940; 256 KiB 3248 / 3056 → 3280 / 3044; 512 KiB 3564 / 3356 → 6624 / 3376; `hello` 2936 / 2808 → 2784 / 2780. So it holds up to 3 MB more ten seconds after the load and the same a minute after. A large `--compile`d CLI application that sits at a prompt (anonymous + file-backed RSS in MB; 10 minutes at the first prompt, a scripted session of 20 turns, 10 minutes parked; two runs each, all four started together): | | main | this PR | |---|---|---| | first prompt + 30 s | 121+95 / 122+95 | 124+67 / 124+95 | | + 2 min | 100+31 / 100+38 | 101+36 / 101+36 | | + 5 min | 96+32 / 96+38 | 97+36 / 97+37 | | + 10 min | 96+32 / 96+39 | 97+37 / 97+38 | | after the session + 30 s | 258+87 / 252+79 | 265+87 / 268+88 | | + 2 min | 190+43 / 183+44 | 197+43 / 195+44 | | + 5 min | 165+44 / 160+45 | 171+44 / 170+45 | | + 10 min | 171+51 / 166+53 | 176+52 / 176+53 | At the prompt that is 3 MB more for the first minute, 1 MB at two minutes and after. After the session these four runs are 5–10 MB apart; the same comparison with one binary and the floor switched by the environment (`…LARGE_FLOOR=0`) came out at 171 / 161 against 168 / 173 at + 5 min, so what the floor itself keeps there (3–4 MB for two minutes) is inside what two runs of one configuration differ by. Instructions (`perf stat -e instructions:u`, pinned, interleaved, median [range]): | | main | this PR | |---|---|---| | `bun -e 1` (9 runs) | 10.05 M [10.04–10.07] | 10.05 M [10.05–10.07] | | `JSON.stringify` + `JSON.parse` loop (5) | 4788.6 M [4786.5–4793.8] | 4789.3 M [4788.8–4796.6] | | allocation loop (5) | 5760.0 M [5753.6–5766.7] | 5799.9 M [5758.2–5824.1] | | the allocator alone: 50 M random `malloc`/`free` | 1.9932 G | 1.9932 G | | the allocator alone: 20 M × `malloc(128 KiB)` + `free` | 5.0313 G | 5.0313 G | (The allocation loop moves by that much between runs of one binary; its ranges overlap.) ## Test `test/js/bun/jsc/heapStats-mimalloc.test.ts`: a `Bun.serve` child that echoes 256 KiB bodies, requests 50 ms apart with the sweep's epoch shortened to 10 ms and the collector looking every 100 ms, minor faults per request read from `/proc`. 2.3–2.4 with this pin, 130–131 with main's (the bound is 20). Linux only, not ASAN.
This was referenced Sep 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #48.
The regression
Since #46 the idle sweep discards the free blocks of large pages (blocks of 96 KiB and up), held back only for the two epochs of the sweep after the last allocation from the page (one to two
purge_holes_min_interval, 100–200 ms). A thread that handles a request now and then, and parks in between, gets every such buffer back as discarded memory and pays a page fault for each OS page of it on every request.Bun.servethat answers a request with its own body, one connection, 5 requests a second, 20 s (Linux x64, release; minor faults of the server process from/proc/<pid>/stat, kernel instructions fromperf stat -e instructions:k -p; both per request):MIMALLOC_PURGE_HOLES=0)(The machine was loaded by other work while this was measured, so there are no latencies here: the median of a 256 KiB request moved between 1.1 and 1.8 ms from run to run of one binary. At 512 KiB it shows through: a median of 2.42 ms on bun-dev3-v2 and 2.67 ms with a floor of 0, 1.93 and 1.77 ms with this PR, 2.20 ms with hole purging off.)
Where the faults come from: about half are free blocks in a page that still has a block in use, which is what #48 is about. The other half is not free blocks at all. The buffers of a request come back to a page of their own once the collector has freed them; that page is freed when its last block is freed (or the sweep's collect finds it with no block in use and frees it), and the next request makes and faults in a new one: four new large pages for every ten requests of 256 KiB. Nothing that only looks at free blocks reaches that.
The rule
swept_state. An epoch lasts forpurge_holes_min_intervalat least and ends only when a thread is swept. Nothing is kept by the clock and nothing is woken for this: a process in which no thread parks any more keeps those bytes (4 MiB at the most) until one does. Withpurge_holes_min_interval0 there are no epochs and no floor.swept_statewhich thread counts it, so that the other threads that sweep the same heap neither discard it nor count it again.MI_RETIRE_CYCLESand no longer; the collect that an idle thread does right before it is swept leaves it to that sweep. For a thread that is never swept, and with a floor of 0, such a page is freed where it is found, as before.MIMALLOC_PURGE_HOLES_LARGE_FLOORin the environment: a bare number is bytes, as for every size option (8192is 8 KiB). Write4MiBor4096KiB. It is in the option's comment.What it costs
A large bundled CLI application (a
--compiled program of some 200 MB that sits at a prompt; anonymous + file-backed RSS in MB; 10 minutes parked at the first prompt, a scripted session of 20 turns, 10 minutes parked; two runs each, started together):The parked application moves the epoch about twice a second by its own wakeups (read from the running process), so 256 epochs are about two minutes there and 512 about four: that is why the default is 256. It is 1 MB over a floor of 0 two minutes after it was last busy (2–3 MB with 512 epochs) and equal to it from five minutes on; the anonymous column after the session moves by ±5 MB from run to run of one configuration, and what is kept there (3–4 MB at first) is inside that. The first turn after a 60 s pause takes the same number of instructions with and without the floor (2.5–2.8 G in every configuration, run to run).
A process that goes silent (24 blocks of 300 KiB, 20 of them freed, five short parks, then one park for good): it keeps 3.0 MB of the 6 MB it freed (the floor is 4 MiB, a page stays or goes whole) for as long as it sits there: 4460 KiB anonymous at every 10 s reading for 70 s, against 1460 KiB with a floor of 0. The scavenger thread has two voluntary context switches in 62 s, which is its 30 s safety timeout from before; nothing is scheduled for the floor.
An idle server: 10 s after the load stopped it has 0.2–1.0 MB more anonymous memory than with a floor of 0 (3.9 MB against 2.9 MB after the 128 KiB load, 3.3 against 3.1 after 256 KiB); 70 s after, the two are equal in every run (the runtime's own collection at idle frees the buffers and collects). An idle server whose runtime does not do that keeps what it kept: nothing moves the epoch there.
Everything else: a server that only answers
hellohas the same 1.1 faults per request and the same 2.8 MB before and after. Nothing is on the path of an allocation or a free that does not empty a large page: the allocator micro benchmarks take the same number of user instructions as on bun-dev3-v2 to the last digit shown (50 M randommalloc/freeof 16–1039 bytes: 1.9932 G; 20 M ×malloc(128 KiB)+free: 5.0313 G; 10 M ×mi_heap_malloc+free: 0.5509 G; clang 21 release,perf stat -e instructions:u, pinned, interleaved). The question whether a page waits for the sweep is asked where_mi_page_retirewould otherwise free a page, in a function of its own; asked inline it cost the 128 KiB pair 5%. Process start and exit take 6 K instructions more (two options).Tried and dropped
swept_stateand a fourth park state. The owner of this fork preferred no timer at all; 4 MiB in a silent process is the price.Tests
test-purge-holes-large:floor(what stays is bounded by the floor; comes out the same at the next sweep; is counted once when another thread sweeps the same heap; a thread that ends gives its share back),floor-decay(pages that were used later take the floor from the ones before; time alone takes nothing; nothing stays once the pages were not allocated from for that many epochs),floor-free-page(a page with no block in use stays, the next allocation of its size takes it, and it goes; no floor without an interval). The floor of a case is derived from what the case frees, so the geometry of a large page on the platform (4 MiB with 320 KiB blocks, or 1 MiB with 80 KiB blocks on Windows) does not matter.test-park-handoff: a thread that stays parked keeps what is under the floor and nobody is woken for it; it goes once the parks of another thread have moved the epoch far enough, though the first thread never left its park.Each of the two less obvious pieces was checked by taking it out: without the keeper mark on abandoned pages
floorfails (the second sweeper discards what the first kept), and without the second look at a parked thread thetest-park-handoffcase fails.