-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Labels
Milestone
Description
Describe the issue:
There seems to be a memory leak introduced in numpy 2.3.0. I've noticed this when using numpy.cumsum
with the out=
parameter. It looks as if data for the array passed in the out
parameter is not being released.
Reproduce the code example:
import numpy as np
import pytest
# (also needs pytest-memray installed)
@pytest.mark.limit_leaks("100 KB")
def test_does_not_leak() -> None:
for _ in range(100):
size = 1000
x = np.ones(size, np.float64)
v = np.empty(size, np.float64)
np.cumsum(x, out=v)
Error message:
Test was allowed to leak 100.0KiB per location but at least one location leaked more
----------------------------------------------------------------------------------------------------- memray-leaked-memory ------------------------------------------------------------------------------------------------------
List of leaked allocations:
- 781.2KiB allocated here:
default_malloc:<unknown>:0
PyDataMem_UserNEW:<unknown>:0
PyArray_NewFromDescr_int:<unknown>:0
PyArray_Empty_int:<unknown>:0
array_empty:<unknown>:0
...
Python and NumPy Versions:
Tested on python 3.11
numpy:
- numpy 2.3.0 leaks
- numpy 2.2.6 does not leak
Runtime Environment:
>>> numpy.show_runtime()
[{'numpy_version': '2.3.0',
'python': '3.11.13 (main, Jun 9 2025, 16:09:23) [GCC 13.1.1 20230614 (Red '
'Hat 13.1.1-4)]',
'uname': uname_result(system='Linux', node='ab21b3c99130', release='6.6.87.2-microsoft-standard-WSL2', version='#1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL',
'AVX512_SPR']}},
{'architecture': 'Haswell',
'filepath': '/dependencies/opt/bb/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so',
'internal_api': 'openblas',
'num_threads': 22,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.29'}]
Context for the issue:
This is problematic for long running python services, as it can result in unbounded memory growth and eventual service failure.