Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/foundation/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
#include <malloc/malloc.h> /* malloc_zone_pressure_relief */
#else
#include <unistd.h>
#if defined(__GLIBC__)
#include <malloc.h> /* malloc_trim */
#endif
#endif

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

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

Expand Down
8 changes: 5 additions & 3 deletions src/foundation/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ void cbm_mem_collect(void);
size_t cbm_mem_footprint(void);

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

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