A refused reservation that frees nothing counts toward the memory limit's end
Date: 2026-11-29
Area: gc, engine
Supersedes: the reset rule of ADR 0185 ("A collection that ends
within the limit resets the count") for the collection a reservation runs; ADR 0185's other decisions stand.
Related: ADR 0202,
Garbage Collector, Errors,
#2355
Context
ADR 0185 ends a run that catches every memory-limit refusal and keeps what it allocated: each refusal records what its
full collection left, eight in a row that leave no less end the run with the uncatchable MemoryLimitError, and a
collection that ends within the limit resets the count.
ADR 0202 then made an allocation whose size the script chose (an array's elements, a buffer's bytes, a built string)
reserve its bytes first (HeapReserve). A reservation that does not fit runs a full collection and, if it still does
not fit, is refused with a catchable RangeError at once. That collection ends within the limit, because the heap is
below it and only the request is not, so it reset the count, and the refusal itself counted nothing.
A script that catches every refusal and keeps what it holds then never reached the eighth refusal whenever the
refusals it met were reservations': scripts/test-cli.ts's sync loop ran all 100,000 iterations, each paying for a
full collection, and ended with no error after about two minutes. Which kind of refusal a script meets depends on the
heap's accounting to the byte: at --max-memory=16777216 it met safe-point refusals until #2348 added a few kilobytes
of charged bytes, and at 14, 23 and 29 MB it met reservations' before. The guarantee that a ceiling the guest can catch
is not one it can ignore in a loop did not hold.
Three answers were weighed:
- Count every refused reservation. A script that keeps asking for one request larger than the whole limit, which no collection could ever make room for, would end the same way, though it holds nothing.
- Count a refused reservation that frees nothing, but not one larger than the whole limit.
- Keep the reset and change the test, which hides the gap rather than closing it.
Decision
Johannes chose the second (29 November 2026, "Count, not oversized").
- A refused reservation is a refusal. When a reservation of no more than the limit does not fit after the full
collection it ran, and that collection ended within the limit,
HeapReserverecords it (ReserveRefusedBytes). When that refusal reaches the script, asThrowMemoryLimit's catchableRangeErroror a fetch's rejected body (NoteReserveRefusal), it counts as ADR 0185's refusals do: a strike when its collection left no less than the last refusal's, and the last of the row ends the run there, past every handler, as a spent limit (AbortKind3,MemoryLimitError). - Only a refusal the script gets counts. A native that retries a smaller request and fits (a string builder's halved growth) clears the record: the script was refused nothing.
- Its collection does not reset the count unless the reservation then fits:
CollectMajorleaves the clear toHeapReservewhile a reservation's collection runs. A collection that ends within the limit anywhere else resets the count only when it leaves less than the last refusal's did, soGoccia.gc()in the script'scatch, which frees nothing, does not undo the row. For refusals at a safe point this is unchanged: their last collection left the heap over the limit. - Its end names the request:
An allocation of N bytes does not fit under the memory limit of M bytes, where an end that a collection leaving the heap over the limit reaches still readsLive heap of N bytes exceeds the memory limit of M bytes. - A request larger than the limit itself never counts and resets nothing. It stays a catchable
RangeError, and a script that keeps asking for one runs on. The line is the limit: a request a little smaller, which the engine's own heap (about 2 MB of built-ins) already leaves no room for, counts like any other.
Consequences
- A script that catches every refusal and keeps what it holds ends with
MemoryLimitErrorwhichever kind of refusal it meets. The six shapes ofscripts/test-cli.ts's check end that way at every limit from 8 to 48 MB (3 runs at each 1 MB step), each within a second; the check runs the two that meet reservations at 14, 16, 23 and 29 MB, and checks that a script holding the heap near the limit and asking for 1 MiB in a catch loop ends naming the request. - A script that holds the heap near the limit and keeps asking for a request an empty heap would hold ends after the
row of refusals, where it ran on before; a test that caught 51 such refusals in a row now catches 7. One that asks
for more than the limit in a catch loop runs on as before, and
scripts/test-cli.tschecks that too. - Safe-point refusals and reservation refusals share one count (
CountMemoryRefusal). The row counts from its first refusal, so the eighth ends the run, as "eight refusals in a row" says; before, the first set the baseline and the ninth ended it.scripts/test-cli.tschecks that seven leave the run going and the eighth ends it.