rpmalloc icon indicating copy to clipboard operation
rpmalloc copied to clipboard

Memory leak issue with multiple threads in iOS game scenario

Open ryanzh1990 opened this issue 2 months ago • 0 comments

Hi, I encountered a serious memory issue when using rpmalloc in our iOS game. The allocator reserves excessive virtual memory and maybe fails to release it properly, especially in a multi-threaded environment.

Issue Details:

  1. Each thread reserves 256MB span(s) even for small allocations
  2. Memory is not properly released back to the system after rpfree
  3. With multiple threads, virtual memory usage grows dramatically

Reproduction Steps:

  1. Create multiple threads (e.g., 20 threads, similar to a typical game scenario)
  2. In each thread:
void thread_func() {
    // Small allocation triggers a 256MB span reservation
    void* ptr1 = rpmalloc(32);
    
    // Medium allocation triggers another 256MB span
    void* ptr2 = rpmalloc(256 * 1024);
    
    // Large allocation triggers yet another 256MB span
    void* ptr3 = rpmalloc(32 * 1024 * 1024);
}

Questions:

  1. Is this the intended behavior?
  2. Should there be a mechanism to release unused pages?
  3. Would it be better to have different span sizes for different allocation patterns? Especially for inactive threads that don't allocate much memory.

Any guidance or potential fixes would be greatly appreciated.

ryanzh1990 avatar Nov 04 '25 09:11 ryanzh1990