Skip to content

page: a collect that is not forced leaves a large page with no block in use to the sweeps - #52

Merged
Jarred-Sumner merged 1 commit into
oven-sh:bun-dev3-v2from
robobun:robobun/2dc95571/collect-leaves-floor-pages
Sep 16, 2026
Merged

Jarred-Sumner merged 1 commit into
oven-sh:bun-dev3-v2from
robobun:robobun/2dc95571/collect-leaves-floor-pages

Conversation

@robobun

@robobun robobun commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Problem

  • A large page with no block in use, which a sweep left under purge_holes_large_floor (page: a few MB of recently used large pages are left alone for a while (no timer) #50), is freed by the next collect that is not forced. The next burst of buffers faults the whole page in again. Measured in bun with 6 buffers of 512 KiB every second: 12 of 25 bursts re-fault all 770 pages. The rule of page: a few MB of recently used large pages are left alone for a while (no timer) #50 says 0.
  • The cause is mi_theap_page_collect (src/theap.c:111). It frees a page with no block in use when retire_expire == 0, and it asks _mi_page_purge_holes_large_page_waits only for the collect of an idle thread. The sweep sets retire_expire = 0 on each page that stays.
  • Such collects are frequent. mi_malloc_generic_admin does one every generic_collect allocations. JavaScriptCore does one after each full collection (bmalloc::api::scavengeThisThread(false)), on the thread that allocates the buffers.

Fix

  • A page that stays under the floor says so in swept_state (MI_PAGE_SWEPT_KEPT), also when it is one of the thread's own pages. Before, only abandoned pages had the mark.
  • A collect that is not forced leaves a page with the mark of its own thread alone. Those pages are counted in holes_floor_kept, so all of them together stay within the floor.
  • The same collect retires a waiting page that has no mark, as _mi_page_retire does. A thread that is never swept again holds it for MI_RETIRE_CYCLES, not longer.
  • Verified: test-purge-holes-large (floor-free-page, two new checks fail without the change). All 34 release and 35 debug tests pass.

Background

Notes

Cost. A thread whose runtime collects when idle used to give the kept pages back at that collect. Now they stay until 256 epochs pass, a forced collect runs, or the thread ends. A Bun.serve echo server, idle after 100 requests (anonymous RSS in KiB at +10 s / +70 s / +310 s):

body before this PR floor 0
128 KiB 2932 / 2036 / 2020 2808 / 2808 / 2792 2032 / 2032 / 2020
512 KiB 2424 / 2424 / 2408 5680 / 5680 / 5668 2420 / 2420 / 2408

This is the price that #50 states for a process in which no thread parks any more (4 MiB at most). #50 measured less for an idle server only because the collect at the end of the idle garbage collection freed the kept pages.

Gain (release builds of bun, the allocator the only difference, Linux x64, minor faults):

before this PR
6 x 512 KiB every second, bursts that re-fault all pages 12 of 25 0 of 25
6 x 384 KiB 12 of 25 0 of 25
echo server, 512 KiB body, 5 requests a second, faults per request 10.5 2.2
echo server, 512 KiB body, one request every 2 s 144.1 3.8
echo server, 128 KiB body, one request every 2 s 19.1 2.5
echo server, 256 KiB body, one request every 2 s 14.8 3.8

How the cause was found. A trace in _mi_page_free and mi_theap_page_collect shows collect=0 (MI_NORMAL), retire_expire=0, state MI_PAGE_SWEPT_ALLOC, on the owner thread. A gdb breakpoint on mi_theap_collect shows the caller: JSC::Heap::didFinishCollection -> bmalloc::api::scavengeThisThread -> mi_theap_collect(theap, false). With one 512 KiB block kept in use, the same script has 0 of 25: nothing frees a page that has a block in use.

The mark.

  • mi_page_holes_floor_stays writes MI_PAGE_SWEPT_KEPT | keeper | stamp. The stamp is still the epoch of the last allocation. The next allocation writes MI_PAGE_SWEPT_ALLOC over it.
  • _mi_page_abandon changes the mark of an own page back to MI_PAGE_SWEPT_ALLOC. So the mark on an abandoned page still means that a sweep passed it as an abandoned page, and the first sweeper to pass counts it.
  • _mi_page_purge_holes_large_page_is_kept compares only the thread part of the keeper. The count of ended threads in the mark is for abandoned pages.
  • A page that another thread kept and this thread reclaimed has the other thread's mark. The collect retires it.

Not changed. A working set of more than one large page still keeps little. A full large page is abandoned. The next burst extends the thread's own small page (new memory, 960 faults for 15 blocks of 256 KiB) before it reclaims the abandoned page whose free blocks the floor kept. With this PR that case alternates strictly: 16 x 256 KiB every 500 ms has a mean of 540 faults per burst (401 before this PR, 1031 before #50). That is allocation policy, not the sweep.

…in use to the sweeps

A large page with no block in use that a sweep left under purge_holes_large_floor was freed by the next collect that
was not forced (`mi_theap_collect(theap, false)`). `mi_theap_page_collect` asked `_mi_page_purge_holes_large_page_waits`
only for the collect of an idle thread, and the sweep sets `retire_expire` to 0 on a page that stays, which is what the
other collects free. Every `generic_collect` generic allocations do such a collect, and a runtime does one at the end
of a garbage collection: JavaScriptCore after every full collection, on the thread that allocates the buffers. A page
whose buffers are all free between two bursts was gone in a third to a half of the gaps of one second, and the next
burst faulted all of it in again. A page with a block in use is not affected: nothing frees that one.

- A page that stays under the floor says so in `swept_state` (`MI_PAGE_SWEPT_KEPT`, which only abandoned pages had),
  also as one of the thread's own. The epoch of its last allocation stays under the mark, and the next allocation
  from the page writes its stamp over it, as before. `_mi_page_abandon` takes the mark of an own page off, so the
  mark on an abandoned page still means that a sweep passed it as an abandoned page.
- A collect that is not forced leaves a page with the mark of its own thread alone. Those pages are counted
  (`holes_floor_kept`), so all of them together stay within the floor, also for a thread that is never swept again.
- It retires a page that waits for a sweep and has no such mark (in the hold, or found with no block in use by this
  collect), as `_mi_page_retire` does where the last block is freed. A thread that is not swept any more holds that
  page for `MI_RETIRE_CYCLES` and no longer; the collect freed it at once before.
- A forced collect, the end of the thread and `purge_holes_large_floor_epochs` epochs without an allocation take the
  page as before.

test-purge-holes-large `floor-free-page`: 40 collects that are not forced leave the page under the floor (it is still
there, and the next sweep keeps the same bytes), one such collect in the hold leaves it, 40 of them and no sweep free
a page in the hold, a forced collect frees the page under the floor. The first two fail without the change.
@Jarred-Sumner
Jarred-Sumner merged commit eab0901 into oven-sh:bun-dev3-v2 Sep 16, 2026
23 checks passed
robobun added a commit to oven-sh/bun that referenced this pull request Sep 16, 2026
Bumps oven-sh/mimalloc from 2014682 to 7b4eb15, the head of
oven-sh/mimalloc#34. That is bun-dev3-v2 (which adds oven-sh/mimalloc#52
over the current pin) plus one commit.

Each WebAssembly.Memory reserves 4 GiB of address space through
mi_malloc_aligned. mimalloc placed every huge allocation at a new
hinted address, and each new 512 MiB region of the 2-level page map
cost a 64 KiB sub-map, 4 KiB of it resident, that is never freed. The
fork commit skips the hint above 1 GiB so the OS reuses the range that
was just unmapped.

Fixes #41459
robobun added a commit to oven-sh/bun that referenced this pull request Sep 16, 2026
… no address hint for OS allocations above 1 GiB

Bumps oven-sh/mimalloc from 2014682 to 3a13a5f.

- os: no address hint for OS allocations above 1 GiB (oven-sh/mimalloc#34)
- page: a collect that is not forced leaves a large page with no block in
  use to the sweeps (oven-sh/mimalloc#52)

Each WebAssembly.Memory reserves 4 GiB of address space through
mi_malloc_aligned. mimalloc placed every huge allocation at a new
hinted address, and each new 512 MiB region of the 2-level page map
cost a 4 KiB sub-map that is never freed. Skipping the hint above
1 GiB lets the OS reuse the range that was just unmapped.

Fixes #41459
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Sep 16, 2026
… collect after a garbage collection (#42853)

## What

`MIMALLOC_COMMIT` 20146828 → eab09015, the head of `bun-dev3-v2`: one
fork PR, oven-sh/mimalloc#52.

Since #42808 the allocator's idle sweep leaves up to 4 MiB of recently
used large pages (buffers of 96 to 512 KiB) alone, so that a server does
not fault the buffers of every request in again. A page that stays with
*no* buffer in use was freed all the same by the next collect that is
not forced, and JavaScriptCore does one after every full garbage
collection (`bmalloc::api::scavengeThisThread(false)`), on the thread
that allocates the buffers. The next request faulted the whole page in
again. oven-sh/mimalloc#52: such a page carries the mark of the thread
that keeps it, and only the sweeps (after 256 epochs), a forced collect
and the end of the thread take it.

## Numbers

Release builds of this tree (`f5649a7edb4`), the mimalloc commit the
only difference, Linux x64. `Bun.serve` that answers a request with its
own body, one connection; minor faults of the server from
`/proc/<pid>/stat` and kernel instructions from `perf stat -e
instructions:k -p`, per request:

| | main: faults | kernel instr | this PR: faults | kernel instr |
|---|---|---|---|---|
| 128 KiB, 5 requests a second | 1.9 | 256 K | 2.5 | 476 K |
| 256 KiB, 5 a second | 2.5 | 305 K | 2.5 | 294 K |
| 512 KiB, 5 a second | 6.0 | 775 K | 3.5 | 835 K |
| 128 KiB, one request every 2 s | 12.7 | 2410 K | 6.7 | 897 K |
| 256 KiB, one every 2 s | 35.1 | 1273 K | 10.7 | 776 K |
| 512 KiB, one every 2 s | 61.0 | 1485 K | 14.8 | 1640 K |
| `hello`, 5 a second | 1.1 | 121 K | 1.1 | 101 K |

(20 s at 5 a second, 40 s and 20 requests at one every 2 s, so the slow
rows are few requests each; the fork PR's own table, with more of them,
has 144 → 3.8 for 512 KiB every 2 s and 12 of 25 → 0 of 25 bursts of 6 ×
512 KiB that fault all of their 770 pages in again.)

## Cost

What #42808 stated and did not pay, because that collect gave the pages
back: a server that goes completely idle holds what the floor kept until
a thread parks again. Anonymous RSS of the server in KiB, 10 s / 70 s
after the load stopped:

| after | main | this PR |
|---|---|---|
| 128 KiB bodies | 3188 / 2964 | 4084 / 3824 |
| 256 KiB bodies | 3264 / 3092 | 5048 / 4868 |
| 512 KiB bodies | 3520 / 3388 | 7376 / 7184 |
| `hello` | 2824 / 2748 | 2892 / 2792 |

3.8 MB more at the most (the floor is 4 MiB).
`MIMALLOC_PURGE_HOLES_LARGE_FLOOR=0` turns the floor off.

A large bundled CLI application (a `--compile`d program of some 200 MB
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 | 105+98 / 106+98 | 106+98 / 106+98 |
| + 2 min | 89+45 / 89+45 | 91+45 / 90+45 |
| + 5 min | 85+46 / 85+47 | 86+46 / 86+46 |
| + 10 min | 85+45 / 85+45 | 86+46 / 86+46 |
| after the session + 30 s | 250+92 / 275+92 | 292+92 / 254+91 |
| + 2 min | 185+46 / 199+46 | 181+46 / 190+46 |
| + 5 min | 159+47 / 164+47 | 154+47 / 164+47 |
| + 10 min | 165+56 / 169+57 | 159+56 / 169+56 |

1 MB more at the prompt, the same after the session (that application
moves the sweep's epochs by its own wakeups, about two a second, so what
it keeps goes after two minutes).

`test/js/bun/jsc/heapStats-mimalloc.test.ts` passes 8/8 three times with
this pin (release build).
robobun added a commit to oven-sh/bun that referenced this pull request Sep 16, 2026
…e sweep kept (oven-sh/mimalloc#52)

Bursts of four 512 KiB buffers with Bun.gc(false) between them, which is
mi_collect(false), the collect that JavaScriptCore runs after each full
collection. With the pin before #42853, 11 of 12 bursts fault all 512
pages of their buffers in again. With the current pin none does.

Linux only, not ASAN, and only where a page is 4 KiB (the test counts
faults in pages of that size). linuxPageSize() in the harness reads
AT_PAGESZ from /proc/self/auxv.
robobun added a commit to oven-sh/bun that referenced this pull request Sep 16, 2026
…een quiet

Since oven-sh/mimalloc#52 (#42853) the collect at the end of a
JavaScriptCore collection leaves the large pages that mimalloc keeps for
the next burst (up to 4 MiB). That collect was what gave them back in an
idle process, so an idle server now holds them for as long as it is idle.

GarbageCollectionController runs mi_collect(true) when it requests an idle
full collection (after BUN_IDLE_GC_SECONDS in which the JS heap did not
grow, at most three times in a quiet period, main thread only), and once
more at the next timer fire, for what that collection has freed since.

Test: the page of six 512 KiB buffers stays while the process is active,
also across Bun.gc(false), and is gone after the idle collection. Linux
only, not ASAN, 4 KiB pages.
springmin pushed a commit to springmin/bun that referenced this pull request Sep 17, 2026
…ch64

Upstream highlights: LLVM 21.1.8 -> 23.1.1 with Rust nightly 2026-09-15,
WebKit -> c28156899e5f (via c775a5dc527d and d3720d515e14), mimalloc pinned to
oven-sh/mimalloc#52, node:fs/watcher O_CLOEXEC, proxy/TLS identity fixes,
Bun.FetchSession, resolver fd lifetime in watch mode.

Conflict resolutions:
- src/collections/{lib,multi_array_list}.rs: keep the OHOS nightly-2026-07-20
  compat layer (upstream's only change there is the migration to the newer
  nightly's type-info APIs, which this port deliberately does not take).
- scripts/build/workarounds.ts: take upstream's deletions (asan-dyld-shim,
  rust-lld-for-crosslang-lto are obsolete with LLVM 23) and re-add the
  ohos-node-userinfo-preload entry, with its stale shim reference corrected.
- scripts/build/deps/zstd.ts: run upstream's bmi2-probe-once patch and keep the
  OHOS qsort-r patch (different files, no ordering interaction).
- test/napi/node-napi-tests/harness.ts: LLVM 23 /usr/lib paths plus the OHOS
  PATH fallback for the compiler.
- test/js/node/vm/vm.test.ts, test/js/web/fetch/fetch.tls.test.ts: merge the
  import lists (upstream's new helpers + the OHOS tweaks).

OHOS adaptation checks: marker counts unchanged in sys/lib.rs,
run_command.rs, napi_body.rs, crash_handler/lib.rs, multi_run.rs; rust-toolchain
keeps the aarch64-unknown-linux-ohos target (the build pins its own nightly via
RUST_HOME); flags.ts/source.ts OHOS entries intact; webkit.ts OHOS PIC/ICU
paths intact. The build moves to brew llvm 23.1.1 + lld 23.1.1 in a follow-up
commit.
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.

2 participants