Skip to content

Commit 172787d

Browse files
committed
fix(mem): no glibc malloc_trim; the static Linux release links again
The v0.10.9 release run (34948714902) failed in both Linux portable builds: the static link found malloc, free, realloc, calloc, memalign, valloc, pvalloc and posix_memalign defined twice, once by mimalloc's override and once by libc.a(malloc.o). Nothing in the build flags had changed since v0.10.8; what changed was one call. cbm_mem_release_to_os called glibc's malloc_trim(0), and that symbol lives only in malloc.o, so the reference pulled the whole object into the link next to mimalloc's definitions. Dynamic builds never noticed: interposition resolves the duplicates at load time, which is why the whole test matrix was green. The call was pointless where it broke things: on Linux and Windows the override routes every malloc to mimalloc, so glibc's heap holds nothing to trim and mi_collect(true) is the release. The branch and its include are gone; macOS keeps its pressure-relief call, since the system heap still serves everything outside the core there. Verification: the static build (scripts/build.sh CC=gcc CXX=g++ STATIC=1) in the local arm64 container reproduced the CI failure exactly before the change and links after it, the binary reporting its version; mem and parallel suites green on the host. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 2058d49 commit 172787d

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

‎src/foundation/mem.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
#include <malloc/malloc.h> /* malloc_zone_pressure_relief */
3636
#else
3737
#include <unistd.h>
38-
#if defined(__GLIBC__)
39-
#include <malloc.h> /* malloc_trim */
40-
#endif
4138
#endif
4239

4340
/* Does THIS build ask mimalloc to replace ordinary malloc process-wide?
@@ -606,11 +603,15 @@ size_t cbm_mem_footprint(void) {
606603
}
607604

608605
void cbm_mem_release_to_os(void) {
606+
/* mi_collect is the release: on Linux and Windows mimalloc owns malloc, so
607+
* there is no libc heap to trim. glibc's malloc_trim was called here once
608+
* and cost the static Linux release its link: the reference pulls
609+
* libc.a(malloc.o) in beside mimalloc's malloc/free (multiple definition,
610+
* release run 34948714902, 2026-09-15). macOS keeps the system heap for
611+
* everything outside the core, hence the pressure-relief call there. */
609612
mi_collect(true);
610613
#if defined(__APPLE__)
611614
(void)malloc_zone_pressure_relief(NULL, 0);
612-
#elif defined(__GLIBC__)
613-
(void)malloc_trim(0);
614615
#endif
615616
}
616617

‎src/foundation/mem.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ void cbm_mem_collect(void);
119119
size_t cbm_mem_footprint(void);
120120

121121
/* Hand freed memory back to the OS on every allocator this process uses:
122-
* mimalloc (mi_collect), the macOS system zones (malloc_zone_pressure_relief)
123-
* and glibc (malloc_trim). Costs a few ms; call at phase boundaries after a
124-
* bulk release, never per allocation. */
122+
* mimalloc (mi_collect) and, on macOS, the system zones
123+
* (malloc_zone_pressure_relief). Never glibc's malloc_trim: on Linux mimalloc
124+
* owns malloc, and the reference alone breaks the static release link. Costs
125+
* a few ms; call at phase boundaries after a bulk release, never per
126+
* allocation. */
125127
void cbm_mem_release_to_os(void);
126128

127129
/* ── Memory map: where does the process's memory actually live? ──────

0 commit comments

Comments
 (0)