Skip to content

Cap glibc malloc arenas at two on Linux - #71

Merged
themuffinator merged 1 commit into
themuffinator:mainfrom
danperisse-hue:linux-malloc-arena-cap
Nov 27, 2026
Merged

themuffinator merged 1 commit into
themuffinator:mainfrom
danperisse-hue:linux-malloc-arena-cap

Conversation

@danperisse-hue

Copy link
Copy Markdown
Contributor

Follow-up to 00f4b40 (Sys_ReleaseFreeHeapMemory), which trims glibc's malloc arenas after each level load. On d2510380c a Linux client still grows from map to map. Part of that growth is the job workers' own malloc arenas, and capping glibc at two arenas stops it.

Measurements. I ran the game under WSL (Ubuntu 26.04) on Xvfb with GL (llvmpipe), cycling through the same 6 SP maps (roadhouse, feedingtowera, salvage, lotaa, spindlea, spherebrain) for 6 laps. Every 5 s I split RSS by mapping kind from /proc/<pid>/smaps, then compared two unpatched and two patched runs, alternating them.

Run RSS lap 1 → 6 Anonymous mappings (thread arenas) [heap]
unpatched 1 1320 → 1725 MB 201 → 344 MB 996 → 1258 MB
unpatched 2 1325 → 1735 MB 192 → 347 MB 1010 → 1264 MB
arenas = 2, run 1 1277 → 1474 MB (lap 5; the run ended before the lap-6 sample) 126 → 138 MB 1028 → 1213 MB
arenas = 2, run 2 1253 → 1535 MB 131 → 133 MB 998 → 1278 MB
  • With the cap, the thread-arena share stays flat, and each lap starts 50–70 MB lower. By lap 6 the patched runs use about 200 MB less.
  • Map loads are no slower: in 72 paired loads the patched build was faster 57 times, by 72 ms on average (median 2125–2141 ms against 2198–2228 ms).
  • Setting MALLOC_ARENA_MAX=2 in the environment gave the same picture, before I moved the cap into the code.

Change. Posix_EarlyInit calls mallopt( M_ARENA_MAX, 2 ) before any other thread starts. It skips the call when MALLOC_ARENA_MAX is set, so a user's own setting still wins. The change only applies on glibc (__GLIBC__). posix_memory_management now checks the cap and its position, and fails if the cap is changed.

Not addressed here: the main heap keeps growing. [heap] still grows about 50 MB a lap with or without the cap, and with Vulkan (lavapipe) as well as GL. A 12-lap unpatched run went from 1328 to 2058 MB RSS, with [heap] going from 1004 to 1560 MB and still rising at the end. The engine's own reports stay flat across laps: printMemInfo, showDictMemory, showTriSurfMemory, showInteractionMemory, and each map's "total memory used". So the growth is in memory the engine does not account for. I have not traced it yet. I haven't measured a native Linux GPU driver either; the Windows long sessions stay flat.

Checks:

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 (00f4b40) trims them
after each load, but RSS still climbs from map to map.

On d251038 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.
@themuffinator
themuffinator merged commit 7a2395e into themuffinator:main Nov 27, 2026
9 checks passed
themuffinator pushed a commit that referenced this pull request Nov 27, 2026
PREY's game code calls collisionModelManager->SetupTrmModel for trace-model
clip models every frame (idClipModel::Handle, reached from SetWaterLevel,
CheckGround and the weapon traces, and Clip.cpp's contact queries) and never
frees what it gets back: Doom 3's manager refilled one scratch model. The
shim calls ModelFromTrm, which builds a new standalone model on each call,
so every call leaked one.

On Linux x64 a sampling allocation tracer put 40-43 MB per lap of six maps
on ModelFromTrm under idClipModel::Handle, plus about 8 MB in
AddPolygonToNode, AddBrushToNode and AllocNode for the same models. Over six
laps (GL), heap growth went from +262 and +254 MB on main to +70 MB with
this change. With #71 applied too, resident memory grew by 75 MB instead of
about 405 MB.

collision_trm_scratch_model checks that the shim keeps one scratch model
and frees the previous one before making the next, that Clip.cpp still
calls SetupTrmModel without freeing the result, and that the check rejects
the old shim.
themuffinator added a commit that referenced this pull request Nov 29, 2026
…round, and stop rewriting the config at launch

Ported from openPR06, the PREY port built on openQ4 (third round: pull
requests #53-#81 and issues #61-#78 by danperisse-hue).

- A deflated PK4 member keeps what it inflates once it is seeked (up to
  32 MiB), so a wave or model chunk scan no longer inflates the member again
  at every seek, and a seek to the current position does nothing (#53).
- Generated animation caches are built in memory and written, synced and
  closed by a writer thread, at most 16 files and 64 MiB queued; the main
  thread promotes them, and game shutdown waits for the queue (#54, as
  adapted to openQ4's version-3 cache in openPR06 a945a6b).
- idInternalCVar::Update flags CVAR_ARCHIVE only when the declaration makes
  a cvar archived, so a module registering archived cvars the config already
  set no longer rewrites openQ4Config.cfg on every launch (#63).
- idProgram::FindFreeResultDef finds a reusable <RESULT> temporary through a
  per-scope hash chain, newest first like the def list, instead of walking
  every result compiled so far (#64).
- Linux: the loose-directory memo runs on case-sensitive filesystems too,
  resolving case once per directory (#56); glibc malloc arenas are capped at
  two unless MALLOC_ARENA_MAX is set (#71); the fatal-breadcrumb directory is
  created like mkdir -p (#57).
- com_timestampPrints describes 1 as seconds and 2 as milliseconds (#60).
- Tests: filesystem_length_probe_contract follows the memo (#65),
  posix_memory_management checks the arena cap, and android_savepath
  extracts Posix_MkdirParents (openPR06 bd071e1).
- BUILDING.md: running the Linux stock smokes under a headless Weston (#81).

Prey-only or already in openQ4, and not ported: #48, #55, #59, #66-#68,
#70, #73, #74, #76, #77, #79, #80.

Co-authored-by: danperisse-hue <danperisse@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants