[PR #7477] memory: a RAM limit that holds the process, not one that pages #7624

Open
opened 2026-11-03 07:21:02 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7477
Author: @CocofireHD
Created: 11/2/2026
Status: 🔄 Open

Base: main ← Head: feature/hard-memory-cap


📝 Commits (1)

  • caa3407 memory: a RAM limit that holds the process, not one that pages

📊 Changes

8 files changed (+601 additions, -19 deletions)

View changed files

📝 crates/horizon-client/src/game/memory.rs (+6 -0)
📝 crates/horizon-client/src/game/ram_limit.rs (+14 -0)
📝 crates/horizon-client/src/main.rs (+1 -1)
📝 crates/horizon-client/src/ram_limit.rs (+87 -14)
➕ crates/horizon-threads/src/capped.rs (+411 -0)
📝 crates/horizon-threads/src/lib.rs (+5 -1)
📝 crates/horizon-threads/src/memory_limit.rs (+60 -3)
📝 docs/native-client.md (+17 -0)

📄 Description

Why

HORIZON_MEMORY_LIMIT_MB caps the working set. On Windows the kernel enforces that, but it enforces
what stays in RAM: the game keeps its pages and Windows moves them out. Measured here (Windows 11,
Ryzen, RTX 4060, render distance 12, twelve teleports into fresh terrain, one sample a second):

limit 512 MB, today
working set 511–512 MB
private bytes 1750 → 2196 MB
fps 30
render distance stayed 12

The cap does exactly what it says, and the process still holds 2.2 GB. A 512 MB limit costs page-file
traffic, not memory — and that is the difference to -Xmx, which the JVM checks against its own heap
and which a Java game cannot talk its way out of.

What this adds

A second, opt-in limit: HORIZON_MEMORY_HARD_MB, /ramlimit hard <MB>, /ramlimit hard off. It is a
budget of the process's committed memory — the number Task Manager shows — and nothing is paged
out to meet it:

  • Once a second the game compares the process's commit with the budget. Over 90% of it, far terrain
    goes off and the render distance in use steps down, the same governor that already exists, now
    driven by commit instead of page faults (a smooth frame is no excuse to hold more). Below 70% both
    come back.
  • At the lowest distance and still over for 20 seconds, the game ends with the existing crash report,
    naming what it held and the budget, instead of living in the page file.
  • The allocator holds the same line for the second between two looks: it counts the bytes the game
    asks for (a free returns them at once) and refuses a request past the ceiling, so a burst cannot
    pass the budget. That ends the game with a message naming the ceiling.
  • Set alone, the hard limit also becomes the RAM limit, so the system cap and the governor agree.
  • /ramlimit now reports both, and with a hard limit it reports the process's commit, not the
    working set.

Measured with this PR

Same world, same twelve teleports:

hard limit commit render distance outcome
2048 MB peaks 2043, settles 1665–1890 12 → 7 runs, stays under
1400 MB 1452–1612 at distance 2 12 → 2 crash report after 20 s: "it holds 1499 MiB and the limit is 1400 MiB"
700 MB — — allocator: "the hard memory limit of 700 MiB is reached (holding 699 MiB, asked for 128 more bytes)"

The 700 MB run is the honest floor: this build commits 1.1–1.2 GB before a world is open (renderer
and resources), so a budget under ~1.4 GB cannot be met at any render distance. -Xmx too low throws
OutOfMemoryError for the same reason.

Cost

Counting only runs while a ceiling is set. With none, reserve and release do one relaxed load of a
static that never changes and pass the request straight to mimalloc: no shared counter, no
read-modify-write, nothing for a player who never sets HORIZON_MEMORY_HARD_MB.

Village scene of horizon-client --benchmark village --render-distance 12 --offscreen, eight runs of
each, interleaved; base is origin/main, which has no allocator wrapper at all:

median avg fps median chunk load median chunks generated/s
origin/main 2234 2030 ms 311
this branch, no hard limit 2226 1980 ms 322

The spread inside each group is about ±5 %, so the two rows are the same within noise. With a ceiling
on (8192 MB, so the governor never steps in) three interleaved pairs came out 1.3 %, 1.3 % and 2.2 %
slower: the price of the shared counter, paid only when the ceiling is asked for.

Tests

  • cargo test -p horizon-threads --lib: 31 pass, 11 new — the ceiling's accounting (count up and down,
    peak, refusal, a free without its allocation clamped, an allocator request past the ceiling returning
    null, a growth past it keeping the old allocation, lifting the ceiling, the message buffer) and the
    commit budget's three bands and aim.
  • cargo test -p horizon-client-lib --lib ram_limit: 8 pass, including the new parse and answer.
  • Four real runs of the client: the three above plus a 2048 MB run of twelve teleports with the tool
    reading /ramlimit from the chat log.
  • cargo clippy -p horizon-threads -p horizon-client-lib --all-targets: clean.

Caveats

  • The budget counts the whole process, the renderer's own pages included; the allocator ceiling counts
    the bytes the game asked for, so the process can sit a little above it by the allocator's own pages.
  • The governor reacts on its next one-second tick, so the commit can pass the budget by a few MB before
    the first step down.
  • Turning a ceiling on mid-run seeds the counter with what the process already committed, so
    /ramlimit hard <MB> below that figure ends the game at once rather than after a step down; the
    launcher's own path seeds at startup, when the figure is a few MB.
  • On Linux and macOS the commit path works the same; only the working set cap is Windows-only, as before.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/7477 **Author:** [@CocofireHD](https://github.com/CocofireHD) **Created:** 11/2/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feature/hard-memory-cap` --- ### 📝 Commits (1) - [`caa3407`](https://github.com/horizon-rust/horizon/commit/caa34071cd2247920c11feb6a6140f54c4229f2b) memory: a RAM limit that holds the process, not one that pages ### 📊 Changes **8 files changed** (+601 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/memory.rs` (+6 -0) 📝 `crates/horizon-client/src/game/ram_limit.rs` (+14 -0) 📝 `crates/horizon-client/src/main.rs` (+1 -1) 📝 `crates/horizon-client/src/ram_limit.rs` (+87 -14) ➕ `crates/horizon-threads/src/capped.rs` (+411 -0) 📝 `crates/horizon-threads/src/lib.rs` (+5 -1) 📝 `crates/horizon-threads/src/memory_limit.rs` (+60 -3) 📝 `docs/native-client.md` (+17 -0) </details> ### 📄 Description ## Why `HORIZON_MEMORY_LIMIT_MB` caps the working set. On Windows the kernel enforces that, but it enforces what *stays in RAM*: the game keeps its pages and Windows moves them out. Measured here (Windows 11, Ryzen, RTX 4060, render distance 12, twelve teleports into fresh terrain, one sample a second): | limit 512 MB, today | | |---|---| | working set | 511–512 MB | | private bytes | 1750 → 2196 MB | | fps | 30 | | render distance | stayed 12 | The cap does exactly what it says, and the process still holds 2.2 GB. A 512 MB limit costs page-file traffic, not memory — and that is the difference to `-Xmx`, which the JVM checks against its own heap and which a Java game cannot talk its way out of. ## What this adds A second, opt-in limit: `HORIZON_MEMORY_HARD_MB`, `/ramlimit hard <MB>`, `/ramlimit hard off`. It is a budget of the process's **committed** memory — the number Task Manager shows — and nothing is paged out to meet it: - Once a second the game compares the process's commit with the budget. Over 90% of it, far terrain goes off and the render distance **in use** steps down, the same governor that already exists, now driven by commit instead of page faults (a smooth frame is no excuse to hold more). Below 70% both come back. - At the lowest distance and still over for 20 seconds, the game ends with the existing crash report, naming what it held and the budget, instead of living in the page file. - The allocator holds the same line for the second between two looks: it counts the bytes the game asks for (a free returns them at once) and refuses a request past the ceiling, so a burst cannot pass the budget. That ends the game with a message naming the ceiling. - Set alone, the hard limit also becomes the RAM limit, so the system cap and the governor agree. - `/ramlimit` now reports both, and with a hard limit it reports the process's commit, not the working set. ## Measured with this PR Same world, same twelve teleports: | hard limit | commit | render distance | outcome | |---|---|---|---| | 2048 MB | peaks 2043, settles 1665–1890 | 12 → 7 | runs, stays under | | 1400 MB | 1452–1612 at distance 2 | 12 → 2 | crash report after 20 s: "it holds 1499 MiB and the limit is 1400 MiB" | | 700 MB | — | — | allocator: "the hard memory limit of 700 MiB is reached (holding 699 MiB, asked for 128 more bytes)" | The 700 MB run is the honest floor: this build commits 1.1–1.2 GB before a world is open (renderer and resources), so a budget under ~1.4 GB cannot be met at any render distance. `-Xmx` too low throws `OutOfMemoryError` for the same reason. ## Cost Counting only runs while a ceiling is set. With none, `reserve` and `release` do one relaxed load of a static that never changes and pass the request straight to mimalloc: no shared counter, no read-modify-write, nothing for a player who never sets `HORIZON_MEMORY_HARD_MB`. Village scene of `horizon-client --benchmark village --render-distance 12 --offscreen`, eight runs of each, interleaved; base is `origin/main`, which has no allocator wrapper at all: | | median avg fps | median chunk load | median chunks generated/s | |---|---|---|---| | `origin/main` | 2234 | 2030 ms | 311 | | this branch, no hard limit | 2226 | 1980 ms | 322 | The spread inside each group is about ±5 %, so the two rows are the same within noise. With a ceiling on (8192 MB, so the governor never steps in) three interleaved pairs came out 1.3 %, 1.3 % and 2.2 % slower: the price of the shared counter, paid only when the ceiling is asked for. ## Tests - `cargo test -p horizon-threads --lib`: 31 pass, 11 new — the ceiling's accounting (count up and down, peak, refusal, a free without its allocation clamped, an allocator request past the ceiling returning null, a growth past it keeping the old allocation, lifting the ceiling, the message buffer) and the commit budget's three bands and aim. - `cargo test -p horizon-client-lib --lib ram_limit`: 8 pass, including the new parse and answer. - Four real runs of the client: the three above plus a 2048 MB run of twelve teleports with the tool reading `/ramlimit` from the chat log. - `cargo clippy -p horizon-threads -p horizon-client-lib --all-targets`: clean. ## Caveats - The budget counts the whole process, the renderer's own pages included; the allocator ceiling counts the bytes the game asked for, so the process can sit a little above it by the allocator's own pages. - The governor reacts on its next one-second tick, so the commit can pass the budget by a few MB before the first step down. - Turning a ceiling on mid-run seeds the counter with what the process already committed, so `/ramlimit hard <MB>` below that figure ends the game at once rather than after a step down; the launcher's own path seeds at startup, when the figure is a few MB. - On Linux and macOS the commit path works the same; only the working set cap is Windows-only, as before. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#7624
No description provided.