How to implement Garbage Collection in Numpy

2019-01-12 07:44发布

问题:

I have a file called main.py, which references another file Optimisers.py which only has functions in it and is used in a for loop in main.py. These functions have different optimisation functions in them.

This Optimisers.py then references two other similar files with only functions in them as well, which are in while loops. All of these files use numpy.

I believe that is because of the loops with functions calling on and creating arrays in numpy, which is leading to a memory overload. Therefore I cannot finish some optimisation algorithms, or cycle through all the possible coordinates I would like to.

How do I ensure removal of variables in numpy? As I understand it, numpy's C libraries complicate the standard Python process. What does the %reset array command (from the link below) do? And where should I implement it?

P.S. I've read "Releasing memory of huge numpy array in IPython", and gc.collect() does not work either.

回答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.