When I try to fine tune some memory leaks in Python bindings for some C / C ++ functions, I look at some strange behavior related to garbage collection of Numpy arrays.
I created some simplified cases to better explain the behavior. The code was launched using memory_profiler, the output of which follows immediately after. It seems that the Python garbage collection does not work as expected when it comes to NumPy arrays:
@profile
def ndarray_deletion():
import numpy as np
from gc import collect
buf = 'abcdefghijklmnopqrstuvwxyz' * 10000
arr = np.frombuffer(buf)
del arr
del buf
collect()
y = [i**2 for i in xrange(10000)]
del y
collect()
if __name__=='__main__':
ndarray_deletion()
Using the following command, I called memory_profiler:
python -m memory_profiler deallocate_ndarray.py
Here is what I got:
Filename: deallocate_ndarray.py
Line
================================================
5 10.379 MiB 0.000 MiB @profile
6 def ndarray_deletion():
7 17.746 MiB 7.367 MiB import numpy as np
8 17.746 MiB 0.000 MiB from gc import collect
9 17.996 MiB 0.250 MiB buf = 'abcdefghijklmnopqrstuvwxyz' * 10000
10 18.004 MiB 0.008 MiB arr = np.frombuffer(buf)
11 18.004 MiB 0.000 MiB del arr
12 18.004 MiB 0.000 MiB del buf
13 18.004 MiB 0.000 MiB collect()
14 18.359 MiB 0.355 MiB y = [i**2 for i in xrange(10000)]
15 18.359 MiB 0.000 MiB del y
16 18.359 MiB 0.000 MiB collect()
, collect , . , Numpy - C-, ( Python) ?
, del __del__, , del , ( AFAIK). , increment, . - , ?
. OS X 10.10.4, Python 2.7.10 (conda), Numpy 1.9.2 (conda), Profiler Profiler 0.33 (conda-binstar), psutil 2.2.1 (conda).