Host memory: empty the working set while free RAM is under VICTORY_RAM_TRIM_MB #3749
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3749"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Mikey970 on 2026-11-30T05:56:58Z.
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_setinresource.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, besideresidency::refresh):VICTORY_RAM_TRIM_MBis set (unset or0: off, the default; the runs below used2560). While free memory is under that line it callsK32EmptyWorkingSeton our own process, at most once everyVICTORY_RAM_TRIM_EVERY_Sseconds (default 8).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.VICTORY_KEEP_RAM_GB,QUOTA_LIMITS_HARDWS_MIN_ENABLE), and resumes if it is released (#2638). Commit847e683by @tyr0nsur.working set trimmed (VICTORY_RAM_TRIM_MB): …).memory now:line, written once a second, now ends withworking-set trims N.Costs:
K32EmptyWorkingSettakes 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_oftenandtrims_that_gain_little_back_off.How it was tested
In game on this branch's base (
main41adb551+ this change), same PC and route: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 plainmaingot through with 1,006 MiB free at its lowest.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.mainat41adb551with 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 byVICTORY_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 testpassed for the crates it touches: yes.cargo test -p victory-gpu --lib residencygave 9 passed. Clippy forvictory-gpu(--all-targets, and again with--all-features) added no new warnings. Pristinemainalready fails-D warnings:victory-shaderhas 3 errors (unused imports inemit/lower,while_immutable_conditioninanalysis.rs), andvictory-gpuhas 25 pre-existing warnings. None of them are in the lines this PR touches.cargo xtask policy --stagedpassed.Checked for overlap
SetProcessWorkingSetSizeEx(the kept-RAM lock). It does not empty the set.EmptyWorkingSetoutside the manual F7 flush. This PR complements them.🤖 Generated with Claude Code
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Mikey970 on 2026-11-30T06:00:39Z.
Holding this as a draft while I check a possible regression. In two in-game runs of
main41adb551+ 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 witha host command list did not close (The parameter is incorrect. (0x80070057)); a run of plainmainwith 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
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Mikey970 on 2026-11-30T06:16:20Z.
Result of the isolation runs (same PC,
main41adb551+ this PR, Series X, DLSS Performance via Streamline, 375 s Chapter 1 route):main(2 runs)VICTORY_RAM_TRIM_MB=0)a host command list did not closeThe 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
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by tyr0nsur on 2026-11-30T08:34:59Z.
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 on0f9e2cd, 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.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Mikey970 on 2026-11-30T13:54:35Z.
@tyr0nsur thanks, that's a real interaction:
K32EmptyWorkingSetempties pages below a hard minimum that Windows still reports as enabled, so withVICTORY_KEEP_RAM_GBset the trim undid the floor and the later refaults land on whichever thread touches the pages.I took your
847e683onto this branch as-is (fast-forward, your authorship). A due trim is now skipped whileGetProcessWorkingSetSizeExreportsQUOTA_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 onvictory-gpu --all-targetsadds no warning (the CIcheckfailure is the pre-existingunwind_copyone onmain41adb551). I have not run it in-game yet withVICTORY_KEEP_RAM_GBset.🤖 Generated with Claude Code
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Mikey970 on 2026-11-30T14:44:20Z.
Follow-up
9a0d18ae, after a review of the default:VICTORY_RAM_TRIM_MBnow defaults to0(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 used2560.K32EmptyWorkingSettakes every pageable page, including the CPU-visible pages of upload and readback heaps.Tested:
cargo test -p victory-gpu --lib -- residency::9 passed (newtrims_that_gain_little_back_off); no new clippy warnings. Description updated to match.🤖 Generated with Claude Code
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nick0451 on 2026-11-30T14:51:23Z.
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:
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, #3254), but it's worth knowing that the only failure came in the run with two trims.🤖 Generated with Claude Code
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by futurepro62 on 2026-12-01T05:10:01Z.
Data from the same idea run on another tree, in case it helps decide defaults here. I ported KyleS98's automatic hand-back from VIctory-Neo (
mod trimin itsresidency.rs, commits87f89c5,18231b9,bbb0995,d9d2c3d; full report in Neo #39). It uses the same call (K32EmptyWorkingSeton our own process, on a thread of its own), so @nick0451's question about sub-second hitches is one these runs can answer.PC and build: RTX 5070 Ti 16 GB (621.40), Ryzen 7 9800X3D, 31 GB RAM, Windows 11. This fork's
93008750plus local commits plus the port. Series X, 4K output, DLSS Performance, frame generation 4x, free play and story missions (not a fixed route).VICTORY_KEEP_RAM_GB=0, because the 14 GB hard minimum blocked the hand-back, as @tyr0nsur found here.How Neo's version differs from this PR
*.nvph, mapped into the process: 13 GB of a 40 GB working set in Neo's measurement; this PC has the same files) being trimmed in the middle of a build. That guard may be worth having here too.What the logs show (6 runs, about 2 hours, 26 hand-backs)
SetProcessWorkingSetSizeEx(to 8 GB, then restoring the old limits) still took the set to 1.5-3.2 GB. Each hand-back then landed as one long frame (0.5-1.05 s).a host command list did not closein any of the 6 logs. One run ended in a device hang (0x887A0006, relay fence, the submitting thread had been preparing one submission for 4,820 ms) 43 s after a hand-back. That run also hadVICTORY_WINDOWS_BAR_MB=2048on, and I had put it down to #3563 (it came as a minigame screen opened), but with nick0451's 1-of-3 it is worth saying: 1 failure in 6 runs, 43 s after a trim.Method: hand-back times and sizes are from the build's own WARN lines (
victory_gpu::memory). Frame times are from theVICTORY_FPS_LINE5 s lines and a 1 s recorder of the process (working set, private, free RAM). Long frames are counted in 10 s windows centred on each WARN line. One PC, runs not on a fixed route, so per-scene fps is compared by draws per frame only. Not tested: this PR's build itself, and any PC other than this one.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by xteral67 on 2026-12-01T17:16:17Z.
Results with a working-set trim close to this PR, on a PC that runs out of RAM in a large city.
Setup: RTX 3080 Ti 12 GB, 32 GB RAM, Series X profile, 1440p, DLSS quality, FSR 3 frame generation 2x, diagnostics build. One 26-minute drive into the biggest city.
K32EmptyWorkingSet) took 3.06 s on its own thread with no visible stall.Differences in our version, in case they are useful here:
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by andrewpozdnakov7-jpg on 2026-12-01T17:24:21Z.
The recent runs used a compiler quiet window before trimming the working set. xteral67's run used three seconds. futurepro62's run also kept an emergency path for free RAM below a third of the trim line.
000b036e applies that scheduling rule on
9a0d18ae. A due trim defers while a tracked driver compile is running and for three seconds after it finishes. Below a third of the line it can still run, subject to the existing hard working-set minimum and backoff. This is a check at scheduling time, so a compile can still start after it.A focused Windows fixture used the exact scheduling gate and compile finish path. It exercised active, recently finished and idle compiler states, acute pressure and an unexpired backoff. Windows
cargo check --locked -p victory-gpuand staged asset policy passed. Whether this reduces stalls needs a matched run. The PR head still conflicts inhost.rs. e390ba38 carries the three PR commits and the quiet-window change ontomainat5fabb46ewith their original authors. It keeps the current memory sweep and card breakdown, then appends the trim count. Windowscargo check --locked -p victory-gpupassed on this head and staged asset policy checked all three changed files. The branch gives the PR author a clean starting point for a matched run and rebase.Please keep the Git author on cherry-pick or rebase, or add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>if folding the change in.AI-assisted with OpenAI Codex.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:49:09Z.
Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
Pull request closed