Skip to content

Commit 66c32a8

Browse files
committed
fix(windows): release the allocator thread-heap at thread exit
A static MinGW link has no DllMain and registers no TLS callback, so nothing ran at thread exit and every thread left its mimalloc thread-heap behind for the life of the process. After 300 MCP requests the allocator reported 607 thread-heaps, peak equal to total equal to current -- none ever released -- holding 170.5 MiB against a live set of roughly 300 KiB, growing linearly and without bound (#581). POSIX is unaffected because a pthread TSD destructor already calls this, which is precisely why POSIX stayed flat under the identical workload. The heaps were invisible to mi_heap_visit, which only walks the main and calling heaps, so the growth appeared to belong to no allocator at all and survived every allocator option that was tried against it. Signed-off-by: Martin Vogel <[email protected]>
1 parent 2193f40 commit 66c32a8

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎src/foundation/compat_thread.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ static DWORD WINAPI win_thread_wrapper(LPVOID lpParam) {
3030
void *arg = a->arg;
3131
free(a);
3232
fn(arg);
33+
/* Release this thread's allocator heap before the thread dies.
34+
*
35+
* On POSIX a pthread TSD destructor calls this for us, which is why POSIX
36+
* stays flat. A static MinGW link has no DllMain and registers no TLS
37+
* callback, so nothing runs at thread exit and every thread leaves its
38+
* thread-heap behind for the life of the process: 607 heaps after 300
39+
* requests, 170 MiB committed against a live set of ~300 KiB, growing
40+
* without bound (#581). The heaps are invisible to mi_heap_visit -- which
41+
* only sees the main and calling heaps -- which is why the growth looked
42+
* like it belonged to no allocator at all. */
43+
mi_thread_done();
3344
return 0;
3445
}
3546

0 commit comments

Comments
 (0)