fix(core): Properly release and reclaim memory when stopping emulation #90

Merged
Collecting merged 9 commits from fix/memory-allocation into main 2026-01-09 04:42:05 -05:00
Collaborator

This implements a more aggressive memory reclamation strategy when stopping emulation to prevent memory bloat and leaks. The entire purpose is to, instead of waiting for the Operating System to hitch & try to fix the issue itself with trying to reallocate RAM around, we free it immediately after the emulation stops, allowing users to have longer playtimes, and less worry about out-of-memory killers.

I've added a memory audit to ShutdownMainProcess that tracks the process RSS before and after shutdown. To actually give memory back to the OS, we now use MADV_DONTNEED (Linux) and MEM_RESET (Windows) on the device memory buffer, along with calling malloc_trim on Linux.

The Vulkan rasterizer now explicitly nukes its memory allocations, staging pools, and runtimes during destruction. This ensures all VkBuffer and VkImage handles are gone before the underlying memory slabs are freed.

Other changes:

  1. Fixed a double-free crash in HostMemory by letting the unique_ptr handle cleanup automatically.

  2. Reorganized the shutdown sequence to reset major systems (GPU, FS controller, etc.) before destroying the ServiceManager.

  3. Updated Initialize and ReinitializeIfNecessary to handle memory layout changes and avoid redundant allocations.

This implements a more aggressive memory reclamation strategy when stopping emulation to prevent memory bloat and leaks. The entire purpose is to, instead of waiting for the Operating System to hitch & try to fix the issue itself with trying to reallocate RAM around, we free it immediately after the emulation stops, allowing users to have longer playtimes, and less worry about out-of-memory killers. I've added a memory audit to ShutdownMainProcess that tracks the process RSS before and after shutdown. To actually give memory back to the OS, we now use MADV_DONTNEED (Linux) and MEM_RESET (Windows) on the device memory buffer, along with calling malloc_trim on Linux. The Vulkan rasterizer now explicitly nukes its memory allocations, staging pools, and runtimes during destruction. This ensures all VkBuffer and VkImage handles are gone before the underlying memory slabs are freed. Other changes: 1. Fixed a double-free crash in HostMemory by letting the unique_ptr handle cleanup automatically. 2. Reorganized the shutdown sequence to reset major systems (GPU, FS controller, etc.) before destroying the ServiceManager. 3. Updated Initialize and ReinitializeIfNecessary to handle memory layout changes and avoid redundant allocations.
Collecting deleted branch fix/memory-allocation 2026-01-09 04:42:06 -05:00
Sign in to join this conversation.
No reviewers
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
Kintoki/Emulator!90
No description provided.