memory: guest backing store commits 13.5 GB up front on Windows #390

Closed
opened 2026-11-26 09:18:38 +00:00 by github-bot · 1 comment
Owner

@⁠MehmetCambaz on GitHub · 2026-08-29 10:17 UTC · original

<html>

Problem

On Windows the guest backing store is created with CreateFileMappingA and no SEC_RESERVE, so the whole of PhysicalMemory::TotalSize() (13824 MB) is committed the moment the emulator starts, whatever the game actually needs.

src/kernel/memoryAddressSpace.inc:

m_handle = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE,
                              static_cast<DWORD>(m_size >> 32u),
                              static_cast<DWORD>(m_size & 0xffffffffu), nullptr);

Without SEC_RESERVE a pagefile-backed section defaults to SEC_COMMIT, so the entire 13.5 GB is charged to system commit at startup.

This does not waste physical RAM - pages are still only made resident when touched - but it does consume 13.5 GB of the commit budget, which is what decides whether the process can start at all.

Impact

On a 16 GB machine the emulator regularly fails to launch with:

direct-memory backing: CreateFileMapping failed: 0x000005aa   (ERROR_NO_SYSTEM_RESOURCES)

I have to close a browser to get it to start, every time. It also leaves very little commit headroom for the rest of the system while a game runs, which matters more as titles get heavier.

Measurements

Running Tetris Forever (PPSA25646) on build c52bf45, Windows 11, 16 GB:

  |   -- | -- System commit before launch | 21.31 GB System commit while running | 36.98 GB Delta | +15.67 GB Process working set | 2.46 GB

So about 13.5 GB of commit is charged for a game whose entire working set is 2.5 GB.

I also wrote a small standalone program that does exactly what GuestBackingStore does, to check that SEC_RESERVE behaves as expected before proposing anything:

baseline system commit           :    21.46 GB

[current] SEC_COMMIT 13824 MB
mapped view : ok
system commit now : 35.02 GB (delta +13.55 GB)

[proposed] SEC_RESERVE 13824 MB
mapped view : ok
system commit now : 21.52 GB (delta +0.05 GB)

commit 2048 MB on demand : ok
touched all 2048 MB : ok
system commit now : 23.51 GB (delta +2.05 GB)
readback check : ok

Mapping a SEC_RESERVE section costs essentially nothing, committing 2 GB costs 2 GB, and reads through the view still work.

This is Windows only

The POSIX path already behaves this way. memfd_create / shm_open followed by ftruncate and mmap(MAP_SHARED) gives a sparse mapping where pages are allocated on first touch, so Linux and macOS never take the up-front charge. This would bring Windows in line with the platform that already does the right thing, rather than introducing a new model.

Proposed change

  1. Add SEC_RESERVE to the CreateFileMappingA flags.
  2. Commit backing ranges on demand with VirtualAlloc(m_backing_base + offset, size, MEM_COMMIT, PAGE_READWRITE).

The places that touch backing memory all work on an explicit backing_offset and length rather than arbitrary addresses, so there is a small number of chokepoints rather than a scattered change:

  • the zero-fill at memoryAddressSpace.inc:165
  • the copy paths at :223 and :509
  • ReplacePlaceholderView / MapView, when a guest range is mapped

Committing the same range twice is harmless, so the calls can be conservative.

Open questions I would want a maintainer's view on before writing it:

  • Preferred chokepoint: commit when a physical range is allocated, or when a view is mapped? Allocation is tighter, mapping is simpler.
  • Should freed ranges be decommitted (MEM_DECOMMIT), or is it fine to let commit grow to the high-water mark for the session?

Offer

Happy to implement this if the approach looks right. I have the standalone test above and can attach it, and I can verify on Windows with a real title before and after. I have no Linux or macOS machine, but the change would be inside the existing KYTY_PLATFORM_WINDOWS block so those paths would not be touched.

AI use

Investigated with AI assistance (Claude): it read the memory code, wrote the standalone probe, and ran the measurements on my machine. I reviewed the findings, confirmed the SEC_COMMIT versus SEC_RESERVE behaviour in the probe output myself, and verified the failure this causes on my own system.

</html>
**@⁠MehmetCambaz** on GitHub · 2026-08-29 10:17 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/390) <html> <body> <!--StartFragment--><h3 dir="ltr">Problem</h3><p dir="ltr">On Windows the guest backing store is created with <code>CreateFileMappingA</code> and no <code>SEC_RESERVE</code>, so the whole of <code>PhysicalMemory::TotalSize()</code> (13824 MB) is committed the moment the emulator starts, whatever the game actually needs.</p><p dir="ltr"><span role="button" tabindex="0"><span>src/kernel/memoryAddressSpace.inc</span></span>:</p><div><div style=""><div><div style=""><pre>m_handle = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, static_cast&lt;DWORD&gt;(m_size &gt;&gt; 32u), static_cast&lt;DWORD&gt;(m_size &amp; 0xffffffffu), nullptr);</pre></div><div><div></div></div></div></div></div><p dir="ltr">Without <code>SEC_RESERVE</code> a pagefile-backed section defaults to <code>SEC_COMMIT</code>, so the entire 13.5 GB is charged to system commit at startup.</p><p dir="ltr">This does not waste physical RAM - pages are still only made resident when touched - but it does consume 13.5 GB of the commit budget, which is what decides whether the process can start at all.</p><h3 dir="ltr">Impact</h3><p dir="ltr">On a 16 GB machine the emulator regularly fails to launch with:</p><div><div style=""><div><div style=""><pre>direct-memory backing: CreateFileMapping failed: 0x000005aa (ERROR_NO_SYSTEM_RESOURCES)</pre></div><div><div></div></div></div></div></div><p dir="ltr">I have to close a browser to get it to start, every time. It also leaves very little commit headroom for the rest of the system while a game runs, which matters more as titles get heavier.</p><h3 dir="ltr">Measurements</h3><p dir="ltr">Running Tetris Forever (PPSA25646) on build c52bf45, Windows 11, 16 GB:</p><div style="">   |   -- | -- System commit before launch | 21.31 GB System commit while running | 36.98 GB Delta | +15.67 GB Process working set | 2.46 GB </div><p dir="ltr">So about 13.5 GB of commit is charged for a game whose entire working set is 2.5 GB.</p><p dir="ltr">I also wrote a small standalone program that does exactly what <code>GuestBackingStore</code> does, to check that <code>SEC_RESERVE</code> behaves as expected before proposing anything:</p><div><div style=""><div><div style=""><pre>baseline system commit : 21.46 GB [current] SEC_COMMIT 13824 MB mapped view : ok system commit now : 35.02 GB (delta +13.55 GB) [proposed] SEC_RESERVE 13824 MB mapped view : ok system commit now : 21.52 GB (delta +0.05 GB) commit 2048 MB on demand : ok touched all 2048 MB : ok system commit now : 23.51 GB (delta +2.05 GB) readback check : ok</pre></div><div><div></div></div></div></div></div><p dir="ltr">Mapping a <code>SEC_RESERVE</code> section costs essentially nothing, committing 2 GB costs 2 GB, and reads through the view still work.</p><h3 dir="ltr">This is Windows only</h3><p dir="ltr">The POSIX path already behaves this way. <code>memfd_create</code> / <code>shm_open</code> followed by <code>ftruncate</code> and <code>mmap(MAP_SHARED)</code> gives a sparse mapping where pages are allocated on first touch, so Linux and macOS never take the up-front charge. This would bring Windows in line with the platform that already does the right thing, rather than introducing a new model.</p><h3 dir="ltr">Proposed change</h3><ol dir="ltr" start="1"><li style="">Add <code>SEC_RESERVE</code> to the <code>CreateFileMappingA</code> flags.</li><li style="">Commit backing ranges on demand with <code>VirtualAlloc(m_backing_base + offset, size, MEM_COMMIT, PAGE_READWRITE)</code>.</li></ol><p dir="ltr">The places that touch backing memory all work on an explicit <code>backing_offset</code> and length rather than arbitrary addresses, so there is a small number of chokepoints rather than a scattered change:</p><ul dir="ltr"><li style="">the zero-fill at <code>memoryAddressSpace.inc:165</code></li><li style="">the copy paths at <code>:223</code> and <code>:509</code></li><li style=""><code>ReplacePlaceholderView</code> / <code>MapView</code>, when a guest range is mapped</li></ul><p dir="ltr">Committing the same range twice is harmless, so the calls can be conservative.</p><p dir="ltr">Open questions I would want a maintainer's view on before writing it:</p><ul dir="ltr"><li style="">Preferred chokepoint: commit when a physical range is <strong>allocated</strong>, or when a view is <strong>mapped</strong>? Allocation is tighter, mapping is simpler.</li><li style="">Should freed ranges be decommitted (<code>MEM_DECOMMIT</code>), or is it fine to let commit grow to the high-water mark for the session?</li></ul><h3 dir="ltr">Offer</h3><p dir="ltr">Happy to implement this if the approach looks right. I have the standalone test above and can attach it, and I can verify on Windows with a real title before and after. I have no Linux or macOS machine, but the change would be inside the existing <code>KYTY_PLATFORM_WINDOWS</code> block so those paths would not be touched.</p><h3 dir="ltr">AI use</h3><p dir="ltr">Investigated with AI assistance (Claude): it read the memory code, wrote the standalone probe, and ran the measurements on my machine. I reviewed the findings, confirmed the <code>SEC_COMMIT</code> versus <code>SEC_RESERVE</code> behaviour in the probe output myself, and verified the failure this causes on my own system.</p><!--EndFragment--> </body> </html>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:18:38 +00:00
Author
Owner

@⁠Nmzik on GitHub · 2026-08-29 11:09 UTC · original

@⁠MehmetCambaz It will complicate logic for correct behaviour and potentially introduce bugs. Please increase paging file size

**@⁠Nmzik** on GitHub · 2026-08-29 11:09 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/390#issuecomment-5461997953) @⁠MehmetCambaz It will complicate logic for **correct** behaviour and potentially introduce bugs. Please increase paging file size
This discussion has been locked. Commenting is limited to contributors.
No milestone
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
github/KytyPS5--KytyPS5#390
No description provided.