In general, you canโt. Even if you delete all references to the object, the python implementation still has the option of reusing or freeing memory. On CPython, you can call gc.collect() to force garbage collection to start. But while this may return the memory, it does not necessarily return it to the OS.
But : numpy is an extension module that does its own work and manages its own memory.
When I track memory usage in a python process, I see that RAM usage (Resident Set Size) decreases after del(M)
In [1]: import numpy as np In [2]: M = np.zeros((1000,1000)) In [3]: del(M) In [4]:
Immediately after starting IPython:
slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 0.0 0.5 119644 22692 0 S+ 2:37PM 0:00.39 /usr/local/bin/py
After importing numpy (1):
slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 1.0 0.8 168548 32420 0 S+ 2:37PM 0:00.49 /usr/local/bin/py
After creating the array (2):
slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 0.0 1.0 176740 40328 0 S+ 2:37PM 0:00.50 /usr/local/bin/py
After calling del (3):
slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 0.0 0.8 168548 32496 0 S+ 2:37PM 0:00.50 /usr/local/bin/py slackbox:~>
Therefore, in this case, using del() can reduce the amount of RAM used.
Note that there is an exception to this with numpy. Numpy can use memory allocated by another extension library. In this case, the numpy object is marked as numpy does not own memory, and the release remains in another library.