How to implement Garbage Collection in Numpy
Solution 1:
When a numpy array is no longer referenced, it will be automatically freed by the GC. The C objects are wrapped in Python objects, so for you it should not matter how it's implemented.
Make sure that arrays are not referenced in global variables, since those stick around until overwritten or the program exits.
If you need to free an array from a local variable before it goes out of scope you can use del variablename
(or just assign e.g. None), but that will not take care of any other references, just the one named.
For debugging where you are referencing an object, you can use gc.get_referrers(object)
.
P.S. I've read Releasing memory of huge numpy array in IPython and gc.collect() does not work either.
Unless you have cycles or have called gc.disable()
, gc.collect()
will not make the GC happen sooner.