Skip to content

Memory leak issue with multiple threads in iOS game scenario #363

@ryanzh1990

Description

@ryanzh1990

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions