Skip to content

Host memory: empty the working set while free RAM is under VICTORY_RAM_TRIM_MB - #3749

Open
Mikey970 wants to merge 3 commits into
trustno1-coder:mainfrom
Mikey970:pr/ram-trim
Open

Mikey970 wants to merge 3 commits into
trustno1-coder:mainfrom
Mikey970:pr/ram-trim

Conversation

@Mikey970

@Mikey970 Mikey970 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Title: [pr-performance] Host memory: empty the working set while free RAM is under VICTORY_RAM_TRIM_MB

Fixes #

What it changes

Today the process's working set goes back to Windows only on a hard flush (F7, empty_working_set in resource.rs), which someone has to press. When the PC runs low on free physical memory, nothing gives it back on its own. Most of the working set is title memory (sections backed by the page file) whose texture pages are cold once they have been uploaded.

This adds a thread of its own, victory-ram-trim, started once by the once-a-second sweep (host.rs, beside residency::refresh):

  • It samples free physical memory every 250 ms while free memory is under twice the line, and every second otherwise.
  • Opt-in: it runs only when VICTORY_RAM_TRIM_MB is set (unset or 0: off, the default; the runs below used 2560). While free memory is under that line it calls K32EmptyWorkingSet on our own process, at most once every VICTORY_RAM_TRIM_EVERY_S seconds (default 8).
  • Back-off: a trim that frees less than 256 MiB doubles the wait before the next one, up to 2 minutes; a trim that frees more resets it to VICTORY_RAM_TRIM_EVERY_S. Without this, a PC whose free memory stays under the line (a 16 GB one) would trim every 8 s for the whole run.
  • Hard minimum: a due trim is skipped while Windows reports a hard minimum working set (VICTORY_KEEP_RAM_GB, QUOTA_LIMITS_HARDWS_MIN_ENABLE), and resumes if it is released (Play: the kept RAM's hard minimum is let go before the PC runs out #2638). Commit 847e683 by @tyr0nsur.
  • Each trim logs free memory and the working set before the call and 200 ms after it (working set trimmed (VICTORY_RAM_TRIM_MB): …).
  • The memory now: line, written once a second, now ends with working-set trims N.

Costs: K32EmptyWorkingSet takes every pageable page of the process, including the CPU-visible pages of D3D12 upload and readback heaps. Dirty pages are written to the page file, and pages still in use fault back in on whichever thread touches them next, the submitting thread included. tyr0nsur measured 0 → 32,768 faults (0.34 → 24.5 ms) re-reading a hot 128 MiB after a trim. That is why it is opt-in and backs off. The trim call itself runs on its own thread. The thread does not start in unit tests. The switch logic has unit tests: trims_come_under_the_line_and_not_too_often and trims_that_gain_little_back_off.

How it was tested

  • In game on this branch's base (main 41adb55 + this change), same PC and route:

    • Without it (plain main, same settings): the run reached world entry and free physical RAM fell from 4.4 GB to 72 MB within two seconds (working set 19.9 GB, system commit 49 GB); the bench's 1 GiB free-RAM guard stopped it at about 48 s. A cold-cache run of plain main got through with 1,006 MiB free at its lowest.
    • With it: one trim at world entry, working set trimmed (VICTORY_RAM_TRIM_MB): … trims=1 ok=true free_before_mib=2437 free_after_mib=5303 working_set_before_mib=18366 working_set_after_mib=299; the whole 375 s route completed with 2,438 MiB free at the lowest and no errors.
    • Build: main at 41adb55 with this change, release, RTX 4080 16 GB, Ryzen 7 9800X3D, 32 GB RAM, Series X profile, DLSS Performance through Streamline (1080p rendered, 3840x2160 output), ray tracing off, frame generation off, warm pipeline cache from an earlier run, the 375 s Chapter 1 route (interrogation, therapist, flyover, boat, shore) driven by VICTORY_KEYS.
  • GPU and profile: RTX 4080, 32 GB RAM, Series X profile, 1080p rendered to 4K with DLSS. Measured on our fork's build (Mikey970/VIctory-NVIDIA), which had the same change.

  • Before: in two runs of the Chapter 1 flyover, the PC ran out of free RAM (under 1 GiB) at 153–166 s. Each run was stopped by our RAM guard.

  • After: every later run of the 375 s route finished. For example, runs R20, R26 and R41 kept 2.3–3.4 GB free at their lowest point and did 3–13 trims per run (working-set trims N).

  • For shader changes, the wave sizes tested: n/a

  • cargo test passed for the crates it touches: yes. cargo test -p victory-gpu --lib residency gave 9 passed. Clippy for victory-gpu (--all-targets, and again with --all-features) added no new warnings. Pristine main already fails -D warnings: victory-shader has 3 errors (unused imports in emit/lower, while_immutable_condition in analysis.rs), and victory-gpu has 25 pre-existing warnings. None of them are in the lines this PR touches. cargo xtask policy --staged passed.

Checked for overlap

🤖 Generated with Claude Code

…M_TRIM_MB

A thread of its own (victory-ram-trim, started by the once-a-second sweep)
samples free physical memory, every 250 ms while under twice the line, else
every second; under VICTORY_RAM_TRIM_MB (2560; 0: off) it calls
K32EmptyWorkingSet on the process, at most every VICTORY_RAM_TRIM_EVERY_S (8)
seconds, and logs free memory and the working set before and 200 ms after.
Until now the working set was given back only on a hard flush (F7). Most of
it is title memory whose uploaded texture pages are cold; pages still used
come back by soft faults. D3D12 heaps are not touched. The "memory now:"
line gives the trims.

On a 32 GB PC (RTX 4080, Series X profile, 1080p to 4K DLSS) the Chapter 1
flyover took free memory under 1 GiB at 153-166 s in two runs; with the
trims (3-13 a run) every run of the 375 s route finished, 2.3-3.4 GB free
at the lowest. Measured on a fork build, not yet on this base.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Mikey970
Mikey970 marked this pull request as draft November 30, 2026 06:00
@Mikey970

Copy link
Copy Markdown
Contributor Author

Holding this as a draft while I check a possible regression. In two in-game runs of main 41adb55 + this PR + #3749/#3750/#3751 together (RTX 4080, Series X, DLSS Performance via Streamline), the run ended near the end of the 375 s route with a host command list did not close (The parameter is incorrect. (0x80070057)); a run of plain main with the same settings finished clean. The slot-eviction look let go of no texture in either run, and the trim fired 1–3 times, so neither is an obvious cause yet. I am running each change on its own and will mark this ready again (or fix it) with the result.

🤖 Generated with Claude Code

@Mikey970

Copy link
Copy Markdown
Contributor Author

Result of the isolation runs (same PC, main 41adb55 + this PR, Series X, DLSS Performance via Streamline, 375 s Chapter 1 route):

Run Working-set trim Other PRs World entry Route
plain main (2 runs) none none 1 of 2 stopped by the 1 GiB free-RAM guard (72 MB free) the other completed (1,799 MB free at its lowest)
this branch, trim off (VICTORY_RAM_TRIM_MB=0) off #3750/#3751 off or inert stopped by the guard (949 MB free) —
this branch alone in effect (#3750 off) on — 6 trims, 2,528 MB free at the lowest completed 375 s, no errors
with #3750 and #3751 too (2 runs) on, 1 and 3 trims on 2,438 / 2,528 MB free at the lowest completed / ended at the shore with a host command list did not close

The end-of-route error only appeared with #3750's look-aside on, never with this change alone, so I am marking this one ready again; #3750 stays a draft while I look into that.

🤖 Generated with Claude Code

@Mikey970
Mikey970 marked this pull request as ready for review November 30, 2026 06:16
moritz-eckhard-nickel added a commit to moritz-eckhard-nickel/VIctory-NVIDIA that referenced this pull request Nov 30, 2026
… set while free RAM is under VICTORY_RAM_TRIM_MB)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
K32EmptyWorkingSet can remove pages below the floor requested by
VICTORY_KEEP_RAM_GB while Windows still reports that floor as enabled.
Check the effective policy before each due trim. This keeps the hard floor
while it is active and lets trim resume if Windows releases it, as in
trustno1-coder#2638. Clarify that refaults may still cost time on the submitting thread.
@tyr0nsur

Copy link
Copy Markdown
Contributor

One interaction with VICTORY_KEEP_RAM_GB is reproducible without the title. Windows accepted a 256 MiB hard minimum. A background thread then called K32EmptyWorkingSet. After the same 200 ms delay used here, the working set went from 387 MiB to under 1 MiB while the hard-minimum flag and size were still reported as enabled. Private commit stayed at 385 MiB.

In seven fresh-process runs per arm, reading one byte per page in the 128 MiB hot part of a 384 MiB allocation went from 0 faults / 0.344 ms median without trim to 32,768 faults / 24.520 ms after trim. This only measures the page reread. The background call is off the submit thread, but later access to trimmed pages can still cost time there.

Follow-up commit 847e683, based on 0f9e2cd, skips a due trim while Windows reports a hard minimum. The guarded probe kept the working set at 387 MiB and recorded zero faults on the hot reread. It still trims when no hard minimum is active, and the live query allows trimming after the minimum is released by #2638. The 8 residency tests passed with the diagnostics feature and the staged asset-policy check passed.

@Mikey970

Copy link
Copy Markdown
Contributor Author

@tyr0nsur thanks, that's a real interaction: K32EmptyWorkingSet empties pages below a hard minimum that Windows still reports as enabled, so with VICTORY_KEEP_RAM_GB set the trim undid the floor and the later refaults land on whichever thread touches the pages.

I took your 847e683 onto this branch as-is (fast-forward, your authorship). A due trim is now skipped while GetProcessWorkingSetSizeEx reports QUOTA_LIMITS_HARDWS_MIN_ENABLE (logged once for each minimum it sees), and trimming resumes once the minimum is released (#2638). The doc comment now also says refaults can still cost time on the submitting thread.

Tested: cargo test -p victory-gpu --lib -- residency:: 8 passed; clippy on victory-gpu --all-targets adds no warning (the CI check failure is the pre-existing unwind_copy one on main 41adb55). I have not run it in-game yet with VICTORY_KEEP_RAM_GB set.

🤖 Generated with Claude Code

…ns little

`VICTORY_RAM_TRIM_MB` is now off unless set (default 0; the measured runs
used 2560). On a PC whose free memory stays under the line (a 16 GB one)
it emptied the whole working set every 8 s for the rest of the run, each
time writing dirty pages out and faulting the hot ones back in. A trim
that frees under 256 MiB now doubles the wait before the next one, up to
2 minutes; one that frees more resets it (`next_trim_wait`, test
`trims_that_gain_little_back_off`). The doc no longer says D3D12 heaps
are untouched: `K32EmptyWorkingSet` takes every pageable page, the
CPU-visible pages of upload and readback heaps among them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mikey970

Copy link
Copy Markdown
Contributor Author

Follow-up 9a0d18ae, after a review of the default:

  • Opt-in: VICTORY_RAM_TRIM_MB now defaults to 0 (off). With 2560 on by default, a PC whose free memory stays under the line (a 16 GB one) would empty the whole working set every 8 s for the whole run, paging dirty pages out and faulting hot ones back in (cf. @tyr0nsur's 0 → 32,768 faults on a 128 MiB reread). The runs in the description used 2560.
  • Back-off: a trim that frees less than 256 MiB doubles the wait before the next one, up to 2 minutes; a trim that frees more resets it.
  • Doc fix: the doc no longer says D3D12 heaps are untouched. K32EmptyWorkingSet takes every pageable page, including the CPU-visible pages of upload and readback heaps.

Tested: cargo test -p victory-gpu --lib -- residency:: 9 passed (new trims_that_gain_little_back_off); no new clippy warnings. Description updated to match.

🤖 Generated with Claude Code

@nick0451

nick0451 commented Nov 30, 2026 •

Copy link
Copy Markdown

Test on a 32 GB PC, where free RAM really does drop under the line. RTX 3090 with the card budget capped to emulate an 8 GB card (VICTORY_VRAM_BUDGET_MB=6800), i9-14900K, 32 GB (31.8 GiB), Windows 11. Series S, the city street save standing with a tap every 25 s, 20 min per run, window off screen.

Builds: v2026.11.29.05 (41adb551) against the same with this PR merged (0f9e2cde; the PR has moved on since).

The trim works as described. In each PR run it fired at about minute 8, when free memory reached about 2.5 GB:

run free RAM before → after working set before → after working set mean over the run mean fps
PR, run 1 2,344 → 4,753 MiB (2 trims) 21,279 → 3,443 MiB about 10 GB ~19.5
PR, run 2 2,547 → 5,222 MiB 22,125 → 2,642 MiB 9.9 GB 19.6
PR, run 3 2,485 → 4,753 MiB 22,025 → 3,094 MiB 10.4 GB 20.9
main — — 22.3 GB 19.5
  • Private memory is unchanged (about 25 GB). The pages go to the page file and come back as needed. In my 30 s samples there was no frame-rate dip around a trim, though a sub-second hitch wouldn't show at that rate.
  • One thing to look at: PR run 1 ended about 14 min in, roughly 3 min after its second trim, with a host command list did not close (The parameter is incorrect. (0x80070057)) (host GPU failure). The two later PR runs, with one trim each, ran the full 20 minutes. No main run under the same 8 GB cap failed in about 10 runs over the last two days. So it is 1 of 3 here and 0 of about 10 on main. That is not enough to blame the trim (the same failure is reported on 8 GB cards without it, Runtime crash on 0x80070057 (host command list did not close) when texture slots run out — 4070 Ti SUPER 16GB, quality + FG 3X, v2026.11.29.03/.04 #3254), but it's worth knowing that the only failure came in the run with two trims.

🤖 Generated with Claude Code

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.

3 participants