Skip to content

page: a few MB of recently used large pages are left alone for a while (no timer) - #50

Merged
Jarred-Sumner merged 5 commits into
bun-dev3-v2from
claude/large-page-hold
Sep 15, 2026
Merged

Jarred-Sumner merged 5 commits into
bun-dev3-v2from
claude/large-page-hold

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Sep 15, 2026 •

Copy link
Copy Markdown
Collaborator

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.serve that 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 from perf stat -e instructions:k -p; both per request):

body bun-dev3-v2 (#46) this PR (4 MiB, 256 epochs), two runs this PR, floor 0 hole purging off (MIMALLOC_PURGE_HOLES=0)
128 KiB faults 65 1.8, 3.1 62 1.6
kernel instructions 520 K 220 K, 256 K 498 K 265 K
256 KiB faults 124 4.5, 4.4 127 2.4
kernel instructions 1138 K 297 K, 330 K 1134 K 344 K
512 KiB faults 309 20, 8.0 308 1.8
kernel instructions 2262 K 846 K, 852 K 2203 K 715 K

(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

A large page (its free blocks, or all of it when no block is in use) is left alone until it has not been allocated from for purge_holes_large_floor_epochs epochs of the sweep (256), for up to purge_holes_large_floor bytes process-wide (4 MiB), the pages that were used last first. Past either, the two epochs of the hold are all it gets, as before.

  • No timer, no clock. The age of a page is the epoch of its last allocation, which page: hole purging in large (4 MiB) pages, held back while the page is in use (by the sweep's epoch: no clock on the allocating side) #46 already keeps in swept_state. An epoch lasts for purge_holes_min_interval at 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. With purge_holes_min_interval 0 there are no epochs and no floor.
  • 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 or freed since counts until then: the sum is stale on the high side only (the exceptions, all bounded, are in the comments where they arise).
  • What was used last. The candidate pages of one sweep are decided together at the end of its pass over the thread's own pages, by age. The abandoned pages of its heaps 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 the same heap neither discard it nor count it again.
  • What was written to. Only blocks on the free list count. The part of a page that was never formed was never written to; it is discarded as before, and a page with two blocks of 256 KiB does not count for 4 MiB.
  • A thread that stays parked does not hold the floor against the ones that run. Once what it kept is old enough to go, which is when the sweeps of other threads have moved the epoch that far, the scavenger (which is awake then: some thread parked) sweeps it again.
  • A page with no block in use, until a sweep gets to decide: where its last block is freed it is retired like a small page instead of freed, so a thread that was swept once and does not park any more holds it for MI_RETIRE_CYCLES and 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_FLOOR in the environment: a bare number is bytes, as for every size option (8192 is 8 KiB). Write 4MiB or 4096KiB. 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):

this PR (4 MiB, 256) 4 MiB, 512 epochs floor 0
first prompt + 30 s 122+92 / 126+93 122+96 / 125+95 121+92 / 121+91
+ 2 min 101+44 / 101+44 102+45 / 103+45 100+43 / 100+43
+ 5 min 96+26 / 97+31 96+29 / 96+32 95+30 / 96+33
+ 10 min 97+30 / 97+31 96+30 / 96+32 96+30 / 96+33
after the session + 30 s 268+85 / 255+86 299+86 / 270+86 282+85 / 266+87
+ 2 min 196+43 / 187+43 190+43 / 197+43 194+43 / 198+43
+ 5 min 171+43 / 161+44 164+43 / 171+44 168+43 / 173+44
+ 10 min 177+51 / 166+52 168+52 / 177+52 173+52 / 178+52

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 hello has 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 random malloc/free of 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_retire would 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

  • A flat floor that stays for good (page: the first purge_holes_large_floor bytes of free blocks in large pages stay #48): it counts the never-formed part of a page against the budget (two or three pages use up 8 MiB that nobody wrote to: 125 → 94–105 faults per request in the table above), does not reach pages with no block in use, and costs the parked application 1–2 MB for ever.
  • A wall-clock stamp in the page and a one-shot wake of the scavenger when the floor is over: it gave the memory back in a process that has gone silent, at the price of a clock in swept_state and a fourth park state. The owner of this fork preferred no timer at all; 4 MiB in a silent process is the price.
  • Counting a page with no block in use against a per-thread budget where its last block is freed: the same page is charged every time it empties again. The retirement that small pages already have does the same job without an account.

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 floor fails (the second sweeper discards what the first kept), and without the second look at a parked thread the test-park-handoff case fails.

…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
Jarred-Sumner merged commit 6df7aa1 into bun-dev3-v2 Sep 15, 2026
23 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant