Skip to content

Memory leak on executables embedded with 3.13 #113055

Description

@neonene

Bug report

Bug description:

Since 67807cf, _testembed.exe (x64 Release) increases the memory usage in the cycle of Py_Initialize and Py_Finalize.

I monitored the Task Manager (taskmgr.exe) on Windows10/11, with a change against Programs/_testembed.c:

-#defineINIT_LOOPS 4
+#defineINIT_LOOPS 200

Commands:

  • _testembed.exe test_repeated_simple_init or
  • python -m test test_embed -m test_simple_initialization_api (no log)

With RADIX TREE (as-is)

Loop67807cfmain3.11.7
10067MB210MB4MB
200135MB406MB4MB

No RADIX TREE (as-is)

Loop67807cfmain3.11.7
10010MB168MB4MB
20016MB336MB4MB

Recent obmalloc.c looks ready for finalization. Just adding my rough (invalid?) experiment made the leaks even. So, I hope that they share the same issue. Otherwise, ea2c001 or 15d4c9f has another leak. #98359 (comment)

patch
void_PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState*interp)
{
if (has_own_state(interp)) {
Py_ssize_tleaked=_PyInterpreterState_GetAllocatedBlocks(interp);
assert(has_own_state(interp) ||leaked==0);
interp->runtime->obmalloc.interpreter_leaks+=leaked;
+OMState*state=&interp->obmalloc;
+for (uinti=0; i<maxarenas; ++i) {
+if (allarenas[i].address==0) {
+continue;
+ }
+_PyObject_Arena.free(_PyObject_Arena.ctx,
+ (void*)allarenas[i].address, ARENA_SIZE);
+allarenas[i].address=0;
+--narenas_currently_allocated;
+ }
+PyMem_RawFree(allarenas);
}
}

With RADIX TREE (patched)

Loop67807cfmain
10042MB40MB
20080MB80MB

No RADIX TREE (patched)

Loop67807cfmain
1003.5M3.3M
2003.7M3.4M

cc @ericsnowcurrently@nascheme

CPython versions tested on:

3.12, 3.13, CPython main branch: 3aea6c4

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-subinterpreterstype-bugAn unexpected behavior, bug, or error

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions