From 4e1959737d0d8acfb5df0de46ac05a1db027808a Mon Sep 17 00:00:00 2001 From: danperisse-hue Date: Thu, 26 Nov 2026 13:31:18 -0300 Subject: [PATCH] Cap glibc malloc arenas at two on Linux glibc gives every thread that allocates a malloc arena of its own. The job workers allocate during level loads and the main thread frees, so each arena keeps its pages. Sys_ReleaseFreeHeapMemory (00f4b400) trims them after each load, but RSS still climbs from map to map. On d2510380c under WSL/Xvfb (GL, llvmpipe), with 6 laps of the same 6 SP maps, alternating two unpatched and two patched runs: unpatched lap 1 -> 6 RSS 1320 -> 1725, 1325 -> 1735 MB anonymous (thread arenas) 201 -> 344, 192 -> 347 MB arenas = 2 lap 1 -> 6 RSS 1253 -> 1535 MB (run 1 ended at 1474 after lap 5) anonymous 126 -> 138, 131 -> 133 MB The thread-arena share stops growing, and each lap starts 50-70 MB lower. Map loads got no slower: 57 of 72 paired loads were faster, by 72 ms on average. Posix_EarlyInit sets M_ARENA_MAX to 2 before any other thread starts, unless MALLOC_ARENA_MAX is set, so a user's own setting still wins. posix_memory_management checks the cap and its place. The main heap still grows about 50 MB a lap either way, with the engine's own reports (printMemInfo, showDictMemory, showTriSurfMemory, per-map totals) flat across laps. That part is not addressed here. --- src/sys/posix/posix_main.cpp | 10 ++++++++++ tools/tests/posix_memory_management.py | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/sys/posix/posix_main.cpp b/src/sys/posix/posix_main.cpp index c1e923ac8..4e0b656f3 100644 --- a/src/sys/posix/posix_main.cpp +++ b/src/sys/posix/posix_main.cpp @@ -1226,6 +1226,16 @@ Posix_EarlyInit =============== */ void Posix_EarlyInit( void ) { +#if defined( __GLIBC__ ) + // glibc gives every thread that allocates a malloc arena of its own. The + // job workers allocate during level loads and the main thread frees, so + // each arena keeps its pages and RSS climbs from map to map even after + // Sys_ReleaseFreeHeapMemory. Two arenas keep that in check; set + // MALLOC_ARENA_MAX to choose another number. + if ( getenv( "MALLOC_ARENA_MAX" ) == NULL ) { + mallopt( M_ARENA_MAX, 2 ); + } +#endif posix_mainThread = pthread_self(); posix_mainThreadRecorded = true; memset( &asyncThread, 0, sizeof( asyncThread ) ); diff --git a/tools/tests/posix_memory_management.py b/tools/tests/posix_memory_management.py index 70e3ec4a5..9d58571ce 100644 --- a/tools/tests/posix_memory_management.py +++ b/tools/tests/posix_memory_management.py @@ -98,10 +98,23 @@ def validate_level_load_heap_release() -> None: raise AssertionError("level load must release free heap memory after the settle frames") +def validate_malloc_arena_cap() -> None: + posix = read("src/sys/posix/posix_main.cpp") + body = function_body(posix, "void Posix_EarlyInit( void ) {") + require(body, "#if defined( __GLIBC__ )", "glibc malloc arena cap") + require(body, 'if ( getenv( "MALLOC_ARENA_MAX" ) == NULL ) {', "user MALLOC_ARENA_MAX wins") + require(body, "mallopt( M_ARENA_MAX, 2 );", "glibc malloc arena cap") + cap = body.find("mallopt( M_ARENA_MAX, 2 );") + thread = body.find("posix_mainThread = pthread_self();") + if cap == -1 or thread == -1 or cap > thread: + raise AssertionError("the malloc arena cap must be set before Posix_EarlyInit does anything else") + + def main() -> None: validate_posix_page_locking() validate_ram_total_helpers() validate_level_load_heap_release() + validate_malloc_arena_cap() print("posix_memory_management: ok")