ENH: add _clear_internal_caches function to simplify memory leak debugging#31321
ENH: add _clear_internal_caches function to simplify memory leak debugging#31321MaartenBaert wants to merge 1 commit into
Conversation
…gging This is analogous to sys._clear_internal_caches in cpython
|
We do have a couple of more caches like this, or Python object caches. I OK with adding such a function if it helps you especially if you want to use it for testing downstream. If we want to semi-expose it, we should probably import it to a module other than @ngoldbaum do you have a quick thought? |
|
Maybe it would be better to simply disable these caches with an environment variable that gets checked once when NumPy is imported? That would add one extra comparison with an integer in the allocation fast path but I doubt that's a big deal. We already have a numpy/numpy/_core/src/multiarray/alloc.c Lines 35 to 48 in 200f11c Of course that doesn't help anyone who can't build their own NumPy, but also I'd be surprised if anyone doing this sort of memory leak debugging couldn't build their own NumPy. I'd be fine with adding a more generic |
That works for me! Though if USE_ALLOC_CACHE is exposed as a compile-time option, that works too. |
PR summary
This PR adds a
_clear_internal_cachesfunction to the multiarray module, analogous tosys._clear_internal_cachesin cpython.I added this to simplify debugging the memory leak in #31320 and thought it might make sense to submit this as a separate PR. I'm not sure what the best place for this is and what other caches exist in numpy that may need clearing, I just included the two that were showing up as false positives in my own memory leak testing.
AI Disclosure
The code was written by Claude Sonnet 4.6.