-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-113055: Fix memory leak of obmalloc state #113218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1052,6 +1052,8 @@ _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *interp) | |
return n; | ||
} | ||
|
||
static void free_arenas(PyInterpreterState *interp); | ||
|
||
void | ||
_PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *interp) | ||
{ | ||
|
@@ -1065,6 +1067,7 @@ _PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *interp) | |
assert(has_own_state(interp) || leaked == 0); | ||
interp->runtime->obmalloc.interpreter_leaks += leaked; | ||
} | ||
free_arenas(interp); | ||
} | ||
|
||
static Py_ssize_t get_num_global_allocated_blocks(_PyRuntimeState *); | ||
|
@@ -2612,7 +2615,6 @@ _PyObject_DebugDumpAddress(const void *p) | |
_PyMem_DumpTraceback(fileno(stderr), p); | ||
} | ||
|
||
|
||
static size_t | ||
printone(FILE *out, const char* msg, size_t value) | ||
{ | ||
|
@@ -2666,6 +2668,40 @@ _PyDebugAllocatorStats(FILE *out, | |
|
||
#ifdef WITH_PYMALLOC | ||
|
||
static void | ||
free_arenas(PyInterpreterState *interp) | ||
{ | ||
OMState *state = &interp->obmalloc; | ||
for (uint i = 0; i < maxarenas; ++i) { | ||
// free each obmalloc memory arena | ||
struct arena_object *ao = &allarenas[i]; | ||
_PyObject_Arena.free(_PyObject_Arena.ctx, | ||
(void *)ao->address, ARENA_SIZE); | ||
} | ||
// free the array containing pointers to all arenas | ||
PyMem_RawFree(allarenas); | ||
#if WITH_PYMALLOC_RADIX_TREE | ||
#ifdef USE_INTERIOR_NODES | ||
// Free the middle and bottom nodes of the radix tree. These are allocated | ||
// by arena_map_mark_used() but not freed when arenas are freed. | ||
Comment on lines
+2685
to
+2686
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why aren't they freed when arenas are freed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better performance and simpler code. It might not be too expensive to free them. We would have to track how many nodes are children and then remove when count reaches zero. |
||
for (int i1 = 0; i1 < MAP_TOP_LENGTH; i1++) { | ||
arena_map_mid_t *mid = arena_map_root.ptrs[i1]; | ||
if (mid == NULL) { | ||
continue; | ||
} | ||
for (int i2 = 0; i2 < MAP_MID_LENGTH; i2++) { | ||
arena_map_bot_t *bot = arena_map_root.ptrs[i1]->ptrs[i2]; | ||
if (bot == NULL) { | ||
continue; | ||
} | ||
PyMem_RawFree(bot); | ||
} | ||
PyMem_RawFree(mid); | ||
} | ||
#endif | ||
#endif | ||
Comment on lines
+2683
to
+2702
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not as familiar with the radix tree stuff. I do see the corresponding allocations in |
||
} | ||
|
||
#ifdef Py_DEBUG | ||
/* Is target in the list? The list is traversed via the nextpool pointers. | ||
* The list may be NULL-terminated, or circular. Return 1 if target is in | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've double-checked that this is correct. I didn't see any other allocations for arenas (outside the radix tree stuff).